R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document

INSTALL NECESSARY PACKAGES:

# install.packages("erp.easy")
library(erp.easy)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(ggplot2, quietly = TRUE, warn.conflicts = FALSE)
library(stringr)
library(tidyr)
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths

LOCATE FOLDERS:

# Locate the folder for the EEG output files (.txt) for old and new nets, replace the file location below with the one in your local device:
path_newnets <- "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/newnets/"
path_oldnets <- "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/oldnets/"
# Enter the number of participants in each group:
subs_new <- 70
subs_old <- 12

LOAD DATA:

# Load data into dataframes for each condition separately (the exported .txt files appear separately for each condition):
neg_go <- load.data(path_newnets,"NegGo", subs_new, -100, 999) 
neg_nogo <- load.data(path_newnets,"NegNoGo", subs_new, -100, 999)
neut_go <- load.data(path_newnets,"NeutGo", subs_new, -100, 999)
neut_nogo <- load.data(path_newnets,"NeutNoGo", subs_new, -100, 999)

# Combine all conditions together into a single dataframe:
combo_new <- rbind.data.frame(neg_go, neg_nogo, neut_go, neut_nogo) 
combo_new <- as.data.frame(unclass(combo_new), stringsAsFactors=TRUE)

# Repeat for old nets:
neg_go_old <- load.data(path_oldnets,"NegGo", subs_old, -100, 999) 
neg_nogo_old <- load.data(path_oldnets,"NegNoGo", subs_old, -100, 999)
neut_go_old <- load.data(path_oldnets,"NeutGo", subs_old, -100, 999)
neut_nogo_old <- load.data(path_oldnets,"NeutNoGo", subs_old, -100, 999)

combo_old <- rbind.data.frame(neg_go_old, neg_nogo_old, neut_go_old, neut_nogo_old) 
combo_old <- as.data.frame(unclass(combo_old),stringsAsFactors=TRUE)

head(combo_old)
##    Subject Stimulus Time        V1        V2        V3        V4        V5
## 1 AN122116    NegGo -100 -1.602166 -0.975723 -0.816704 -1.601339 -0.178200
## 2 AN122116    NegGo  -96 -1.381717 -0.857681 -0.553844 -0.892159  0.475065
## 3 AN122116    NegGo  -92 -0.916006 -0.567245 -0.245930 -0.231857  1.066748
## 4 AN122116    NegGo  -88 -0.271272 -0.088409  0.125656  0.340374  1.469912
## 5 AN122116    NegGo  -84  0.449695  0.554581  0.589043  0.840698  1.634542
## 6 AN122116    NegGo  -80  1.147286  1.300797  1.164803  1.323421  1.615967
##          V6        V7        V8        V9       V10       V11       V12
## 1 -0.331926 -0.151961 -0.347968 -0.576614 -1.093344 -1.390728 -1.446274
## 2  0.510233  0.606494 -0.153863 -0.260136 -0.415079 -0.283929 -0.484142
## 3  1.314797  1.333362  0.159202  0.034664  0.071394  0.791458  0.575123
## 4  1.898442  1.850423  0.551349  0.315926  0.373709  1.671227  1.558418
## 5  2.160961  2.056113  0.989324  0.634567  0.603586  2.271284  2.303516
## 6  2.138047  1.978029  1.457013  1.057877  0.921005  2.612692  2.723587
##        V13       V14       V15       V16      V17       V18       V19       V20
## 1 0.154436 -0.183043 -0.986050 -1.668789 0.050217 -2.640511 -3.116714 -2.074911
## 2 0.945122  0.372879 -0.317448 -0.819794 0.983765 -1.523602 -1.794190 -1.187153
## 3 1.697449  0.793301  0.149136  0.003761 1.976685 -0.314388 -0.273128 -0.085423
## 4 2.217609  1.043167  0.441242  0.719955 2.786621  0.844208  1.214652  1.044960
## 5 2.406643  1.174946  0.670528  1.307877 3.234940  1.815136  2.451749  1.997532
## 6 2.313116  1.303911  0.981770  1.808807 3.283058  2.517334  3.306489  2.624617
##         V21       V22       V23       V24       V25       V26       V27
## 1 -1.612476 -3.177330 -4.650845 -2.473480 -1.961811 -4.129242 -2.788494
## 2 -0.844498 -1.744621 -3.119498 -1.602276 -1.253998 -2.740967 -1.987232
## 3  0.098137 -0.120888 -1.237389 -0.474751 -0.290592 -1.055275 -0.902645
## 4  1.032101  1.405754  0.652441  0.778458  0.766792  0.610847  0.338024
## 5  1.764500  2.557701  2.195314  1.949837  1.698437  1.928261  1.515244
## 6  2.181705  3.180214  3.163316  2.833687  2.327084  2.689941  2.407352
##         V28      V29      V30      V31      V32       V33       V34       V35
## 1 -1.600498 0.267609 1.704092 1.655323 1.792316 -1.247337 -1.565639  0.009226
## 2 -0.895760 0.061537 1.575657 1.587364 1.779469 -0.718452 -1.027107 -0.212204
## 3  0.037254 0.093109 1.446121 1.501526 1.689861  0.020538 -0.316589 -0.194126
## 4  1.056332 0.293676 1.282846 1.354988 1.483730  0.877956  0.484066  0.008957
## 5  1.947704 0.504688 1.051548 1.120262 1.165264  1.686760  1.225574  0.244831
## 6  2.520092 0.555014 0.739815 0.802414 0.785526  2.267891  1.750835  0.334261
##        V36      V37      V38      V39       V40      V41      V42       V43
## 1 1.743858 2.122482 1.753445 0.243980 -0.360420 2.080679 2.637558  2.488483
## 2 1.622190 1.938988 1.444054 0.475021 -0.622421 1.851799 2.304211  1.770252
## 3 1.530930 1.721893 1.114063 0.813866 -0.621868 1.605459 1.966421  1.092953
## 4 1.412546 1.461838 0.791637 1.169640 -0.392212 1.336784 1.650398  0.551357
## 5 1.209280 1.153973 0.497448 1.426853 -0.084667 1.030151 1.340042  0.177834
## 6 0.894976 0.806694 0.245754 1.489670  0.100655 0.676033 0.992119 -0.057133
##         V44       V45       V46       V47       V48       V49       V50
## 1 -0.478058 -0.116197  0.471390  0.894775  1.085449 -0.986816 -0.568535
## 2 -0.332985 -0.481936 -0.240822  0.001236  0.034695 -1.011177 -1.498154
## 3 -0.127296 -0.522299 -0.512642 -0.864632 -0.893692 -0.927132 -2.277191
## 4  0.048119 -0.285610 -0.346947 -1.536613 -1.526505 -0.777245 -2.780609
## 5  0.102623  0.035172  0.028740 -1.958992 -1.841530 -0.652672 -3.026688
## 6 -0.007149  0.196474  0.274559 -2.186066 -1.952347 -0.638664 -3.137680
##         V51       V52       V53      V54      V55       V56       V57       V58
## 1  1.070000  1.878197  2.666451 1.737392 1.327618  1.859970  1.614403  2.925186
## 2 -0.160545  0.618912  1.864506 1.494488 1.545944  0.444145  0.366844  1.426971
## 3 -1.294258 -0.504265  1.024616 1.240417 1.603912 -0.640739 -0.757164 -0.111763
## 4 -2.129997 -1.287273  0.292511 0.979656 1.427685 -1.120575 -1.491501 -1.395485
## 5 -2.613663 -1.676357 -0.227856 0.723427 1.046879 -1.017914 -1.773453 -2.266602
## 6 -2.831451 -1.767654 -0.507278 0.491032 0.583645 -0.625381 -1.747795 -2.740128
##         V59       V60       V61       V62       V63       V64       V65
## 1  2.046391  1.169464  1.071705  1.248970  1.905968  1.640926  0.002188
## 2  0.602528  0.107323  0.288998  1.150200  0.342133  0.277851 -0.932522
## 3 -0.905182 -0.946693 -0.552670  0.810631 -1.086975 -1.066990 -1.984392
## 4 -2.166298 -1.769859 -1.264359  0.314499 -2.020744 -2.095554 -2.859217
## 5 -2.966969 -2.223263 -1.700798 -0.188576 -2.319824 -2.660908 -3.353342
## 6 -3.268372 -2.299583 -1.814854 -0.550926 -2.108795 -2.801708 -3.424402
##         V66       V67       V68       V69       V70       V71       V72
## 1 -0.476954  1.102089  0.719635  1.112701 -0.058893 -1.529216 -0.755066
## 2 -1.551037  0.714247  0.691925 -0.130219 -0.911140 -2.128438 -1.079086
## 3 -2.686024  0.118654  0.379486 -1.340378 -1.866013 -2.868699 -1.558258
## 4 -3.593519 -0.555941 -0.125336 -2.261200 -2.673973 -3.511125 -2.041573
## 5 -4.072910 -1.152937 -0.655533 -2.744957 -3.165016 -3.872424 -2.387306
## 6 -4.085543 -1.551479 -1.043991 -2.798599 -3.303223 -3.889922 -2.522308
##         V73       V74       V75       V76       V77       V78      V79      V80
## 1  0.441437  1.033527  0.788539 -0.928551  0.435883  1.081963 2.877953 1.601650
## 2  0.346336  0.178783  0.142629 -1.132133  0.487083  1.028404 2.960096 1.862282
## 3  0.022926 -0.656792 -0.560802 -1.508812  0.197792  0.684425 2.804230 1.968708
## 4 -0.425003 -1.350021 -1.180889 -1.935188 -0.334747  0.144081 2.413523 1.838276
## 5 -0.850845 -1.810688 -1.617779 -2.277023 -0.924843 -0.414665 1.895298 1.485500
## 6 -1.131081 -2.012646 -1.844580 -2.443001 -1.380531 -0.811607 1.405289 1.014156
##        V81       V82       V83       V84       V85      V86      V87      V88
## 1 0.848020  0.460829 -1.231041  0.076736  0.138611 2.784137 3.245194 2.589694
## 2 1.362525 -0.121952 -1.526024  0.125122  0.144287 2.785977 3.160503 2.606918
## 3 1.802489 -0.749256 -1.918602 -0.192474 -0.095490 2.586747 2.883027 2.559120
## 4 2.045585 -1.354576 -2.313644 -0.775436 -0.506226 2.155099 2.388915 2.431592
## 5 2.060138 -1.855576 -2.615715 -1.429977 -0.948303 1.556672 1.757413 2.244977
## 6 1.915174 -2.186262 -2.768425 -1.953003 -1.282578 0.921325 1.127977 2.041994
##         V89       V90       V91       V92      V93      V94       V95       V96
## 1 -3.432014 -4.049820 -0.531204  2.401985 1.829981 3.451154 -7.335152 -3.743881
## 2 -3.773735 -3.862244 -0.350449  2.178871 1.818653 3.347659 -7.251877 -3.261444
## 3 -4.165321 -3.830048 -0.507690  1.777306 1.638340 3.070870 -7.257523 -3.043457
## 4 -4.501930 -3.879913 -0.965775  1.195198 1.223476 2.590276 -7.266975 -3.139648
## 5 -4.653816 -3.891159 -1.573624  0.498116 0.631874 1.963420 -7.130493 -3.444870
## 6 -4.536186 -3.759476 -2.132874 -0.209702 0.001291 1.298814 -6.726074 -3.756775
##         V97       V98      V99      V100      V101     V102     V103     V104
## 1  1.323845  1.687925 1.786144 -4.663246 -2.786003 0.974030 0.679365 1.707811
## 2  1.275421  1.668030 1.726355 -4.882811 -2.492363 0.976803 0.704914 1.570347
## 3  0.907425  1.538475 1.584257 -5.189646 -2.378700 1.009205 0.810230 1.400173
## 4  0.181274  1.197460 1.261652 -5.422186 -2.516228 0.916962 0.854427 1.136353
## 5 -0.772461  0.651726 0.757948 -5.394495 -2.855194 0.627228 0.746681 0.772690
## 6 -1.715271 -0.001589 0.154560 -5.005291 -3.257599 0.161217 0.467112 0.346331
##       V105     V106      V107      V108      V109     V110     V111     V112
## 1 3.157804 0.831240 -0.703478 -1.796181 -0.954175 1.336458 1.670211 1.949009
## 2 3.067249 1.364780 -0.046059 -2.326027 -0.743787 1.344398 1.535836 2.193771
## 3 2.822426 1.789238  0.593539 -2.794525 -0.242855 1.456902 1.459781 2.292099
## 4 2.376984 1.999977  1.056548 -2.981224  0.310246 1.582771 1.452214 2.270863
## 5 1.775845 1.987045  1.255971 -2.768326  0.687672 1.625034 1.492644 2.191158
## 6 1.124949 1.833754  1.224303 -2.219368  0.758452 1.505865 1.527507 2.118137
##        V113      V114      V115      V116     V117     V118      V119      V120
## 1 -0.539551 -1.150284 -1.752474 -1.822952 1.795980 1.812792 -0.461781 -0.497742
## 2  0.007809 -1.588699 -1.885380 -1.500015 1.551477 1.941059 -0.014542 -0.606674
## 3  0.575939 -1.906067 -1.803888 -0.736660 1.388708 1.938408  0.414474 -0.748016
## 4  1.070267 -1.970215 -1.570978  0.139423 1.359306 1.884426  0.785519 -0.875206
## 5  1.428329 -1.725597 -1.263821  0.797108 1.457943 1.859196  1.088673 -0.915016
## 6  1.653008 -1.227242 -0.942726  1.035713 1.612221 1.904932  1.350513 -0.806335
##        V121      V122     V123      V124      V125      V126      V127
## 1 -2.506559 -1.355415 1.636124 -0.138519 -0.575409 -0.063583 -1.093063
## 2 -2.282681 -1.201950 1.676567  0.323805 -0.402016  0.300964 -0.635666
## 3 -1.849167 -0.738174 1.668022  0.681597 -0.205864  0.443298 -0.282318
## 4 -1.320885 -0.027748 1.768433  0.942221 -0.033119  0.364227 -0.125992
## 5 -0.809322  0.793083 2.099190  1.165342  0.105431  0.162506 -0.223267
## 6 -0.376049  1.563683 2.669037  1.429461  0.232117 -0.026382 -0.554886
##        V128     V129
## 1 -0.922371 0.064854
## 2 -0.654320 0.651667
## 3 -0.289268 1.148424
## 4  0.054832 1.419500
## 5  0.246422 1.427877
## 6  0.202545 1.258244

SPECIFY THE ELECTRODE NUMBERS FOR P2, N2 and P3:

FCz = frontrocentral - midline FC4 = frontrocentral - right FC3 = frontrocentral - left

Pz = parietal - midline P4 = parietal - right P3 = parietal - left

# new nets: 
FCz_newnets <- c("V18", "V16", "V10", "V19", "V11", "V4", "V12", "V5", "V6")
FC4_newnets <- c("V3","V123","V124", "V117", "V118") 
FC3_newnets <- c("V23","V27","V24",  "V28", "V20")

Pz_newnets <- c("V54","V79","V61", "V62","V78", "V67", "V72", "V77")
P4_newnets <- c("V85", "V86", "V91" , "V92" ,"V97", "V98")
P3_newnets <- c("V51", "V52","V53", "V59", "V60", "V47")

# repeat for old nets
FCz_oldnets <- c("V19","V16", "V10", "V20","V11", "V4","V12","V5","V6")
FC4_oldnets <- c("V3","V123","V119", "V123","V118")
FC3_oldnets <- c("V24","V25","V21", "V28","V29")

Pz_oldnets <- c( "V54","V62","V80", "V61","V68","V79", "V67", "V73","V78")
P4_oldnets <- c("V87","V99", "V86", "V93", "V98", "V92")
P3_oldnets <- c("V53","V48", "V52", "V60", "V51", "V59") 

CHECK OUT INDIVIDUAL WAVEFORMS:

USING MOSAIC YOU CAN CREATE AN AVERAGE WAVEFORM FOR EACH PARTICIPANT IN EACH GROUP & NET LOCATION:

#Create average waveform plots for each subject in a single, multiplot window
mosaic(combo_new, FCz_newnets, cols = 3, rows = 2)

mosaic(combo_new, Pz_newnets, cols = 3, rows = 2)

mosaic(combo_old, FCz_oldnets, cols = 3, rows = 2)

mosaic(combo_old, Pz_oldnets, cols = 3, rows = 2)

CHECK OUT GRAND AVERAGES USING GRANDAVERAGE()

# grand average plots the grand average waveform for each condition present in the dataframe you provide.
#`invisible()` function to hide the data output.
invisible(grandaverage(combo_new, FCz_newnets))

invisible(grandaverage(combo_new, Pz_newnets))

# butterfly plots all individual waveforms for the condition specified by the stim argument(i.e.,a butterfly plot).
# The grandaverage waveform is also plotted,using a red line.
butterfly(combo_new,FCz_newnets, stim=1)

CODE BELOW GETS ALL THE MEASURES (N2, P2, P3) FROM OLD AND NEW NET DATA, COMBINE THEM TOGETHER AND IT SAVES THE DATA INTO A FINAL COMBO SPREADSHEET:

CHECK THE WINDOW RANGE FOR EACH ERP COMPONENT AND ADJUST AS NEEDED!

P2 between 180–280 ms after stimulus onset at frontal, frontocentral and central electrode sites.

N2 between 320,520 ms after stimulus onset at frontal, frontocentral and central electrode sites.

P3 between 450-750 ms after stimulus onset at parietal electrode sites.

# Get the mean Amplitude measures from the NEW net:
MeanAmp_P2_FCz_newnets <- (m.measures(combo_new, FCz_newnets, window=c(180,280)))

MeanAmp_P2_FC4_newnets <- (m.measures(combo_new, FC4_newnets, window=c(180,280)))

MeanAmp_P2_FC3_newnets <- (m.measures(combo_new, FC3_newnets, window=c(180,280))) 

MeanAmp_N2_FCz_newnets <- (m.measures(combo_new, FCz_newnets, window=c(320,520)))  

MeanAmp_N2_FC4_newnets <- (m.measures(combo_new, FC4_newnets, window=c(320,520)))

MeanAmp_N2_FC3_newnets<- (m.measures(combo_new, FC3_newnets, window=c(320,520)))  

MeanAmp_P3_Pz_newnets <- (m.measures(combo_new, Pz_newnets, window=c(450,750)))  

MeanAmp_P3_P4_newnets <- (m.measures(combo_new, P4_newnets, window=c(450,750)))  

MeanAmp_P3_P3_newnets <- (m.measures(combo_new, P3_newnets, window=c(450,750)))  

# We need to combine these but each one of these datasets use the same variable name - Mean Amplitude. 
# Below is a function that will allow us to rename the variables in multiple datasets in a similar way using different names:

rename_datasets_amplitude <- function(dataset_list, new_col_names){
for (i in 1:length(dataset_list)){
assign(dataset_list[i], rename(get(dataset_list[i]),
!!new_col_names[i] := "Mean Amplitude"), envir = .GlobalEnv)
}
}

datasets <- c("MeanAmp_P2_FCz_newnets", "MeanAmp_P2_FC4_newnets", "MeanAmp_P2_FC3_newnets",
              "MeanAmp_N2_FCz_newnets", "MeanAmp_N2_FC4_newnets", "MeanAmp_N2_FC3_newnets",
              "MeanAmp_P3_Pz_newnets", "MeanAmp_P3_P4_newnets", "MeanAmp_P3_P3_newnets")

new_column_names <- c("MeanAmp_P2_FCz", "MeanAmp_P2_FC4", "MeanAmp_P2_FC3",
                      "MeanAmp_N2_FCz", "MeanAmp_N2_FC4", "MeanAmp_N2_FC3",
                      "MeanAmp_P3_Pz", "MeanAmp_P3_P4", "MeanAmp_P3_P3")

rename_datasets_amplitude(datasets, new_column_names)


#  load multiple datasets into the workspace     
datasets_list <- mget(datasets)

# Using the `Reduce()` function to merge multiple data frames stored in `datasets_list` into a single data frame called `df_merge1`. 
# It does this by merging each data frame in the list with the others, based on the columns "Subject" and "Trial Type".

merge_datasets_amplitude <- function(datasets_list) {
Reduce(function(x, y) {
x <- x[, !(names(x) %in% "Standard Dev")] # ignore Standard Dev column from the first dataframe, do not merge it
y <- y[, !(names(y) %in% "Standard Dev")] # ignore Standard Dev column from the next dataframe, do not merge it
merge(x, y, by=c("Subject", "Trial Type"))
}, datasets_list)
}

# Run the function
df_merge1 <- merge_datasets_amplitude(datasets_list) # use the function to merge the new data frames




# Get the Latency measures from the NEW net:
Latency_P2_FCz_newnets <- (p.measures(combo_new, FCz_newnets, window=c(180,280), pol="pos"))

Latency_P2_FC4_newnets <- (p.measures(combo_new, FC4_newnets, window=c(180,280), pol="pos"))

Latency_P2_FC3_newnets <- (p.measures(combo_new, FC3_newnets, window=c(180,280), pol="pos"))

Latency_N2_FCz_newnets <- (p.measures(combo_new, FCz_newnets, window=c(320,520), pol="neg"))  

Latency_N2_FC4_newnets <- (p.measures(combo_new, FC4_newnets, window=c(320,520), pol="neg"))  

Latency_N2_FC3_newnets <- (p.measures(combo_new, FC3_newnets, window=c(320,520), pol="neg")) 

Latency_P3_Pz_newnets <- (p.measures(combo_new, Pz_newnets, window=c(450,750), pol="pos"))

Latency_P3_P4_newnets <- (p.measures(combo_new, P4_newnets, window=c(450,750), pol="pos"))

Latency_P3_P3_newnets <- (p.measures(combo_new, P3_newnets, window=c(450,750), pol="pos"))

# Function `rename_datasets()` that renames the columns "Peak Latency" and "Peak Amplitude" in each data frame from a list of data frames (`dataset_list`)
rename_datasets_latency <- function(dataset_list, new_col_name1, new_col_name2){
for (i in 1:length(dataset_list)){
temp_data <- get(dataset_list[i])
names(temp_data)[names(temp_data) == "Peak Latency"] <- new_col_name1[i]
names(temp_data)[names(temp_data) == "Peak Amplitude"] <- new_col_name2[i]
assign(dataset_list[i], temp_data, envir = .GlobalEnv)
}
}

datasets <- c("Latency_P2_FCz_newnets", "Latency_P2_FC4_newnets", "Latency_P2_FC3_newnets",
              "Latency_N2_FCz_newnets", "Latency_N2_FC4_newnets", "Latency_N2_FC3_newnets",
              "Latency_P3_Pz_newnets", "Latency_P3_P4_newnets", "Latency_P3_P3_newnets")

new_column_names1 <- c("Latency_P2_FCz", "Latency_P2_FC4", "Latency_P2_FC3",
                      "Latency_N2_FCz", "Latency_N2_FC4", "Latency_N2_FC3",
                      "Latency_P3_Pz", "Latency_P3_P4", "Latency_P3_P3")

new_column_names2 <- c("PeakAmp_P2_FCz", "PeakAmp_P2_FC4", "PeakAmp_P2_FC3",
                      "PeakAmp_N2_FCz", "PeakAmp_N2_FC4", "PeakAmp_N2_FC3",
                      "PeakAmp_P3_Pz", "PeakAmp_P3_P4", "PeakAmp_P3_P3")

rename_datasets_latency(datasets, new_column_names1, new_column_names2)

#  load multiple datasets into the workspace     
datasets_list <- mget(datasets)


merge_datasets_latency <- function(datasets_list) {
Reduce(function(x, y) {
merge(x, y, by=c("Subject", "Trial Type"))
}, datasets_list)
}

# Run the function
df_merge2 <- merge_datasets_latency(datasets_list) # use the function to merge the new data frames

# Combine the 2 dataframes
ERP_newnets <- full_join(df_merge1, df_merge2, by = c("Subject", "Trial Type"))

head(ERP_newnets)
##    Subject Trial Type MeanAmp_P2_FCz MeanAmp_P2_FC4 MeanAmp_P2_FC3
## 1 AE050318      NegGo     -8.6567894     -10.997870      -8.274136
## 2 AE050318    NegNoGo      0.1368089       3.073859     -10.233068
## 3 AE050318     NeutGo    -11.0096655     -10.983267      -8.061495
## 4 AE050318   NeutNoGo     -8.0131387      -7.947222      -8.404834
## 5 AH101121      NegGo     -6.6852204      -9.178219      -7.760717
## 6 AH101121    NegNoGo     -2.2374154      -4.678083      -4.003878
##   MeanAmp_N2_FCz MeanAmp_N2_FC4 MeanAmp_N2_FC3 MeanAmp_P3_Pz MeanAmp_P3_P4
## 1     -17.640501    -17.0612959      -12.45291      4.369997    -1.0128204
## 2      -2.763603     -0.6563204      -11.78551      3.345500     0.4492580
## 3     -18.544452    -17.9227477      -12.32419      4.253425    -0.6663896
## 4     -14.488076    -16.2244929      -12.51434      6.818025    -1.6119402
## 5     -22.625932    -24.2391735      -23.10149     18.547604    11.2572051
## 6     -22.243306    -20.8193210      -24.12986     14.173249    11.9982319
##   MeanAmp_P3_P3 Latency_P2_FCz PeakAmp_P2_FCz Latency_P2_FC4 PeakAmp_P2_FC4
## 1      7.006061            212     -6.2425933            208      -8.224361
## 2      2.221935            252      3.3000709            232       5.512485
## 3      6.126728            220     -8.4652572            220      -9.281374
## 4     12.549254            216     -4.2941628            216      -3.940464
## 5     16.239133            228     -5.6526816            188      -7.083663
## 6      5.765768            240     -0.5854749            204      -1.564159
##   Latency_P2_FC3 PeakAmp_P2_FC3 Latency_N2_FCz PeakAmp_N2_FCz Latency_N2_FC4
## 1            200      -6.982536            352     -23.058663            352
## 2            188      -8.637317            452      -5.257907            400
## 3            212      -6.498847            384     -22.615500            384
## 4            180      -6.557909            320     -20.985998            320
## 5            236      -6.305076            492     -25.080314            484
## 6            240      -1.848016            396     -24.552156            384
##   PeakAmp_N2_FC4 Latency_N2_FC3 PeakAmp_N2_FC3 Latency_P3_Pz PeakAmp_P3_Pz
## 1     -20.626624            368      -17.04337           612      6.037603
## 2      -3.018455            408      -12.41742           600      6.301873
## 3     -21.637845            388      -14.78690           592      6.610696
## 4     -21.846657            324      -16.75670           640     10.395567
## 5     -27.616219            520      -26.08764           528     23.899982
## 6     -27.499941            472      -26.48135           508     18.808306
##   Latency_P3_P4 PeakAmp_P3_P4 Latency_P3_P3 PeakAmp_P3_P3
## 1           576     0.8441270           512      9.862223
## 2           508     3.1160960           648      5.847591
## 3           488     0.3001207           516      8.529244
## 4           508     0.4807528           524     14.750284
## 5           536    15.0651712           512     20.561269
## 6           584    16.8901785           652      4.791160
# remove standard dev column:
# ERP_newnets <- ERP_newnets %>% select(-`Standard Dev`)

REPEAT FOR OLD NETS!

MeanAmp_P2_FCz_oldnets <- (m.measures(combo_old, FCz_oldnets, window=c(180,280)))

MeanAmp_P2_FC4_oldnets <- (m.measures(combo_old, FC4_oldnets, window=c(180,280)))

MeanAmp_P2_FC3_oldnets <- (m.measures(combo_old, FC3_oldnets, window=c(180,280)))

MeanAmp_N2_FCz_oldnets <- (m.measures(combo_old, FCz_oldnets, window=c(320,520)))  

MeanAmp_N2_FC4_oldnets <- (m.measures(combo_old, FC4_oldnets, window=c(320,520)))  

MeanAmp_N2_FC3_oldnets <- (m.measures(combo_old, FC3_oldnets, window=c(320,520)))  

MeanAmp_P3_Pz_oldnets <- (m.measures(combo_old, Pz_oldnets, window=c(450,750)))  

MeanAmp_P3_P4_oldnets <- (m.measures(combo_old, P4_oldnets, window=c(450,750)))  

MeanAmp_P3_P3_oldnets <- (m.measures(combo_old, P3_oldnets, window=c(450,750)))  

datasets <- c("MeanAmp_P2_FCz_oldnets", "MeanAmp_P2_FC4_oldnets", "MeanAmp_P2_FC3_oldnets",
              "MeanAmp_N2_FCz_oldnets", "MeanAmp_N2_FC4_oldnets", "MeanAmp_N2_FC3_oldnets",
              "MeanAmp_P3_Pz_oldnets", "MeanAmp_P3_P4_oldnets", "MeanAmp_P3_P3_oldnets")

new_column_names <- c("MeanAmp_P2_FCz", "MeanAmp_P2_FC4", "MeanAmp_P2_FC3",
                      "MeanAmp_N2_FCz", "MeanAmp_N2_FC4", "MeanAmp_N2_FC3",
                      "MeanAmp_P3_Pz", "MeanAmp_P3_P4", "MeanAmp_P3_P3")

rename_datasets_amplitude(datasets, new_column_names)
                
datasets_list <- mget(datasets)

df_merge1_old <- merge_datasets_amplitude(datasets_list) # use the function to merge the new data frames



Latency_P2_FCz_oldnets <- (p.measures(combo_old, FCz_oldnets, window=c(180,280), pol="pos"))

Latency_P2_FC4_oldnets <- (p.measures(combo_old, FC4_oldnets, window=c(180,280), pol="pos"))

Latency_P2_FC3_oldnets <- (p.measures(combo_old, FC3_oldnets, window=c(180,280), pol="pos"))

Latency_N2_FCz_oldnets <- (p.measures(combo_old, FCz_oldnets, window=c(320,520), pol="neg"))  

Latency_N2_FC4_oldnets <- (p.measures(combo_old, FC4_oldnets, window=c(320,520), pol="neg"))  

Latency_N2_FC3_oldnets <- (p.measures(combo_old, FC3_oldnets, window=c(320,520), pol="neg")) 

Latency_P3_Pz_oldnets <- (p.measures(combo_old, Pz_oldnets, window=c(450,750), pol="pos"))

Latency_P3_P4_oldnets <- (p.measures(combo_old, P4_oldnets, window=c(450,750), pol="pos"))

Latency_P3_P3_oldnets <- (p.measures(combo_old, P3_oldnets, window=c(450,750), pol="pos"))

datasets <- c("Latency_P2_FCz_oldnets", "Latency_P2_FC4_oldnets", "Latency_P2_FC3_oldnets",
              "Latency_N2_FCz_oldnets", "Latency_N2_FC4_oldnets", "Latency_N2_FC3_oldnets",
              "Latency_P3_Pz_oldnets", "Latency_P3_P4_oldnets", "Latency_P3_P3_oldnets")

new_column_names1 <- c("Latency_P2_FCz", "Latency_P2_FC4", "Latency_P2_FC3",
                      "Latency_N2_FCz", "Latency_N2_FC4", "Latency_N2_FC3",
                      "Latency_P3_Pz", "Latency_P3_P4", "Latency_P3_P3")

new_column_names2 <- c("PeakAmp_P2_FCz", "PeakAmp_P2_FC4", "PeakAmp_P2_FC3",
                      "PeakAmp_N2_FCz", "PeakAmp_N2_FC4", "PeakAmp_N2_FC3",
                      "PeakAmp_P3_Pz", "PeakAmp_P3_P4", "PeakAmp_P3_P3")

rename_datasets_latency(datasets, new_column_names1, new_column_names2)

datasets_list <- mget(datasets)

df_merge2_old <- merge_datasets_latency(datasets_list) # use the function to merge the new data frames

                
# Combine the 2 dataframes
ERP_oldnets <- full_join(df_merge1_old, df_merge2_old, by = c("Subject", "Trial Type"))

head(ERP_oldnets)
##    Subject Trial Type MeanAmp_P2_FCz MeanAmp_P2_FC4 MeanAmp_P2_FC3
## 1 AN122116      NegGo     -4.1407086     -1.4327522     -5.0320122
## 2 AN122116    NegNoGo    -11.0193942     -0.1925687     -0.2935563
## 3 AN122116     NeutGo     -5.1375479    -13.6733601      0.6677448
## 4 AN122116   NeutNoGo     -7.0921285     -5.8957279      7.5732563
## 5 AS110816      NegGo     -6.7244279     -8.5284981     -6.5214486
## 6 AS110816    NegNoGo      0.9807348     -1.9055810     -2.6025719
##   MeanAmp_N2_FCz MeanAmp_N2_FC4 MeanAmp_N2_FC3 MeanAmp_P3_Pz MeanAmp_P3_P4
## 1       -8.29314      -1.460082   -10.78825393      2.783801      4.021132
## 2      -20.15702      -1.218012   -13.32166345      5.369831      1.153689
## 3      -13.74024     -17.345666    -6.70710155      9.747895     14.534170
## 4      -16.52584     -16.467716     0.01039033      5.414394      7.557088
## 5      -17.48872     -14.803429   -16.30025058      5.850186     14.521829
## 6      -11.91471     -13.806399   -15.03030459      2.999593      7.822631
##   MeanAmp_P3_P3 Latency_P2_FCz PeakAmp_P2_FCz Latency_P2_FC4 PeakAmp_P2_FC4
## 1    -5.3793184            196      -1.697444            212       2.326486
## 2    -0.3086019            252      -9.920439            256       5.021367
## 3    -1.0471026            212      -1.309128            212      -9.682326
## 4     8.0758937            196      -5.072943            192      -1.011759
## 5     5.8252172            256      -5.428295            252      -7.055694
## 6    -6.5985243            224       3.456288            236       1.125501
##   Latency_P2_FC3 PeakAmp_P2_FC3 Latency_N2_FCz PeakAmp_N2_FCz Latency_N2_FC4
## 1            244     -3.0044314            436      -10.42853            440
## 2            180      7.5681562            376      -25.68119            384
## 3            232      2.9387006            484      -16.53051            380
## 4            244     11.5706090            488      -20.65452            380
## 5            252     -5.2547888            368      -21.73219            360
## 6            224     -0.7118974            352      -17.74692            352
##   PeakAmp_N2_FC4 Latency_N2_FC3 PeakAmp_N2_FC3 Latency_P3_Pz PeakAmp_P3_Pz
## 1      -4.376464            420     -13.546851           520      6.711619
## 2      -9.214241            504     -19.674420           544     13.155540
## 3     -20.075206            464      -8.761441           612     15.601991
## 4     -20.232107            404      -4.058091           708     10.309167
## 5     -18.829883            372     -21.274613           500     12.045843
## 6     -22.411000            348     -20.104096           512      6.997815
##   Latency_P3_P4 PeakAmp_P3_P4 Latency_P3_P3 PeakAmp_P3_P3
## 1           472      7.754448           460      3.051898
## 2           544      7.397239           468     20.779675
## 3           700     16.257373           488      6.043558
## 4           520     12.118001           688     13.128550
## 5           460     23.680317           524      8.128814
## 6           588     10.384612           480     -1.250488

#COMBINE OLD AND NEW DATASETS #CREATE A NEW VARIABLE CALLED N2-P2 DIFFERENCE #RE-SHAPE THE DATA #SAVE INTO A CSV FILE

# Combine old +new
ERP <- full_join(ERP_oldnets, ERP_newnets)
## Joining with `by = join_by(Subject, `Trial Type`, MeanAmp_P2_FCz,
## MeanAmp_P2_FC4, MeanAmp_P2_FC3, MeanAmp_N2_FCz, MeanAmp_N2_FC4, MeanAmp_N2_FC3,
## MeanAmp_P3_Pz, MeanAmp_P3_P4, MeanAmp_P3_P3, Latency_P2_FCz, PeakAmp_P2_FCz,
## Latency_P2_FC4, PeakAmp_P2_FC4, Latency_P2_FC3, PeakAmp_P2_FC3, Latency_N2_FCz,
## PeakAmp_N2_FCz, Latency_N2_FC4, PeakAmp_N2_FC4, Latency_N2_FC3, PeakAmp_N2_FC3,
## Latency_P3_Pz, PeakAmp_P3_Pz, Latency_P3_P4, PeakAmp_P3_P4, Latency_P3_P3,
## PeakAmp_P3_P3)`
# Remove Grand Ave from data, order by subject name and reset the index:
ERP <- ERP[!(ERP$Subject=="Grand Avg"),]
ERP <- with(ERP,  ERP[order(Subject) , ])
rownames(ERP) <- NULL # Reset index

# CREATE A NEW COLUMN by taking the difference between N2-P2
ERP$MeanAmp_N2P2_FCz <- ERP$MeanAmp_N2_FCz - ERP$MeanAmp_P2_FCz
ERP$PeakAmp_N2P2_FCz <- ERP$PeakAmp_N2_FCz - ERP$PeakAmp_P2_FCz

ERP$MeanAmp_N2P2_FC4 <- ERP$MeanAmp_N2_FC4 - ERP$MeanAmp_P2_FC4
ERP$PeakAmp_N2P2_FC4 <- ERP$PeakAmp_N2_FC4 - ERP$PeakAmp_P2_FC4

ERP$MeanAmp_N2P2_FC3 <- ERP$MeanAmp_N2_FC3 - ERP$MeanAmp_P2_FC3
ERP$PeakAmp_N2P2_FC3 <- ERP$PeakAmp_N2_FC3 - ERP$PeakAmp_P2_FC3

# REORGANIZE STIMTAG VARIABLE AS TWO SEPERATE VARIABLES: EMOTION AND CONDITION - EACH WITH TWO LEVELS 
ERP <- ERP %>%
mutate(Emotion = str_extract(`Trial Type`, "Neut|Neg"),
       Condition = str_extract(`Trial Type`, "Go|NoGo"))

# RESHAPE TO LONG FORMAT

ERP_long <- pivot_longer(ERP, 
  cols = -c(Subject, `Trial Type`, `Condition`, `Emotion`), 
  names_to = c(".value",  "Electrode"),
  names_pattern = "^(MeanAmp_P2|MeanAmp_N2|MeanAmp_N2P2|MeanAmp_P3|PeakAmp_P2|PeakAmp_N2|PeakAmp_N2P2|PeakAmp_P3|Latency_P2|Latency_N2|Latency_P3)_(.+)$",
  values_to = "Value"
) %>%
  mutate(
    #Region = ifelse(grepl("^FC", Electrode), "Frontocentral",  "Parietal"),
    Laterality = ifelse(grepl("4", Electrode), "Right", 
               ifelse(grepl("3", Electrode), "Left", "Midline"))
  ) 
#  select(-Electrode)


# MAKE IT COMPACT
ERP_long <- ERP_long %>%
  group_by(Subject, `Trial Type`, Emotion, Condition, Laterality) %>%
  summarise(across(everything(), ~mean(., na.rm = TRUE)), .groups = 'drop')
## Warning: There were 984 warnings in `summarise()`.
## The first warning was:
## ℹ In argument: `across(everything(), ~mean(., na.rm = TRUE))`.
## ℹ In group 1: `Subject = "AE050318"`, `Trial Type = "NegGo"`, `Emotion =
##   "Neg"`, `Condition = "Go"`, `Laterality = "Left"`.
## Caused by warning in `mean.default()`:
## ! argument is not numeric or logical: returning NA
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 983 remaining warnings.
head(ERP_long)
## # A tibble: 6 × 17
##   Subject  `Trial Type` Emotion Condition Laterality Electrode MeanAmp_P2
##   <chr>    <chr>        <chr>   <chr>     <chr>          <dbl>      <dbl>
## 1 AE050318 NegGo        Neg     Go        Left              NA     -8.27 
## 2 AE050318 NegGo        Neg     Go        Midline           NA     -8.66 
## 3 AE050318 NegGo        Neg     Go        Right             NA    -11.0  
## 4 AE050318 NegNoGo      Neg     NoGo      Left              NA    -10.2  
## 5 AE050318 NegNoGo      Neg     NoGo      Midline           NA      0.137
## 6 AE050318 NegNoGo      Neg     NoGo      Right             NA      3.07 
## # ℹ 10 more variables: MeanAmp_N2 <dbl>, MeanAmp_P3 <dbl>, Latency_P2 <dbl>,
## #   PeakAmp_P2 <dbl>, Latency_N2 <dbl>, PeakAmp_N2 <dbl>, Latency_P3 <dbl>,
## #   PeakAmp_P3 <dbl>, MeanAmp_N2P2 <dbl>, PeakAmp_N2P2 <dbl>
# Write to a csv file:
write.csv(ERP_long, "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/ERP_long")

LOAD AND MERGE WITH INTAKE (DEMOGRAPHICS AND CLINICAL ASSESSMENTS) INFO:

# Load DataSet: 
intake <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CognitiveEmotionalLi-IntakeDiagnosticData_DATA_2023-10-25_0818.csv')

# Subject IDs include the visit number in the combo dataset if it is NOT the first time point. 
# Do the same here: Combine visit number with subject and create a new Subject variable so that it matches the combo:
intake <- intake  %>%
  mutate(Subject = ifelse(redcap_event_name !="t1_arm_1", paste0(part_id_status, "T", visitnumber), part_id_status)) 

# Create a new variable representing final sldper100words ("disfluency_sldper100words_final) by taking disfluency_sldper100words from CVD as primary, 
# but in the case that this data is missing, take the disfluency scores from CVE:
intake <- intake  %>%
  mutate(disfluency_sldper100words_final = ifelse(!is.na(disfluency_sldper100words), disfluency_sldper100words, disfluency_sldper100words_cve)) 

# Create a final talker group variable ("talkergroup_final) using disfluency_sldper100words_final and talker group based on parent report:
# 1: CWS, 0:CWNS, 9:unidentified
intake <- intake  %>%
  mutate(talkergroup_final = ifelse((disfluency_sldper100words_final >= 3 | calculator_talkergroup_parent == 1), 1,
                                          ifelse((disfluency_sldper100words_final < 3 & calculator_talkergroup_parent == 0), 0, 9)))  
                    
# Take the relevant columns from intake dataset
# You may update this to take more columns into the dataset:
intake <-  subset(intake, select=c('Subject','calculator_age_cve','calculator_gender_cve','race', 'ethnicity',
                                   'calculator_talkergroup_parent','tso_calculated',
                                   'disfluency_sldper100words','ssi_total', 
                                   'disfluency_sldper100words_final', 'talkergroup_final',
                                   "gfta_standard", "ppvt_standard", "evt_standard",             
                                   'teld_rec_standard','teld_exp_standard', "teld_spokenlang_standard",
                                   'tocs_1_total', 'tocs_2_total', 'tcs_total',
                                   'eprime_condorder_zootask','cve_comments','comments_tasks','handedness_zoo'))

# Merge with the main dataset using SUBJECT
FULL <- merge(ERP_long, intake, by=c("Subject"), all.x = TRUE)
head(FULL)  
##    Subject Trial Type Emotion Condition Laterality Electrode  MeanAmp_P2
## 1 AE050318      NegGo     Neg        Go       Left        NA  -8.2741357
## 2 AE050318      NegGo     Neg        Go    Midline        NA  -8.6567894
## 3 AE050318      NegGo     Neg        Go      Right        NA -10.9978696
## 4 AE050318    NegNoGo     Neg      NoGo       Left        NA -10.2330683
## 5 AE050318    NegNoGo     Neg      NoGo    Midline        NA   0.1368089
## 6 AE050318    NegNoGo     Neg      NoGo      Right        NA   3.0738593
##    MeanAmp_N2 MeanAmp_P3 Latency_P2 PeakAmp_P2 Latency_N2 PeakAmp_N2 Latency_P3
## 1 -12.4529149   7.006061        200  -6.982536        368 -17.043367        512
## 2 -17.6405009   4.369997        212  -6.242593        352 -23.058663        612
## 3 -17.0612959  -1.012820        208  -8.224361        352 -20.626624        576
## 4 -11.7855128   2.221935        188  -8.637317        408 -12.417423        648
## 5  -2.7636031   3.345500        252   3.300071        452  -5.257907        600
## 6  -0.6563204   0.449258        232   5.512485        400  -3.018455        508
##   PeakAmp_P3 MeanAmp_N2P2 PeakAmp_N2P2 calculator_age_cve calculator_gender_cve
## 1   9.862223    -4.178779   -10.060831               38.1                     0
## 2   6.037603    -8.983711   -16.816070               38.1                     0
## 3   0.844127    -6.063426   -12.402263               38.1                     0
## 4   5.847591    -1.552445    -3.780106               38.1                     0
## 5   6.301873    -2.900412    -8.557978               38.1                     0
## 6   3.116096    -3.730180    -8.530940               38.1                     0
##   race ethnicity calculator_talkergroup_parent tso_calculated
## 1    2         0                             1            1.9
## 2    2         0                             1            1.9
## 3    2         0                             1            1.9
## 4    2         0                             1            1.9
## 5    2         0                             1            1.9
## 6    2         0                             1            1.9
##   disfluency_sldper100words ssi_total disfluency_sldper100words_final
## 1                        12        23                              12
## 2                        12        23                              12
## 3                        12        23                              12
## 4                        12        23                              12
## 5                        12        23                              12
## 6                        12        23                              12
##   talkergroup_final gfta_standard ppvt_standard evt_standard teld_rec_standard
## 1                 1           121           126          123               146
## 2                 1           121           126          123               146
## 3                 1           121           126          123               146
## 4                 1           121           126          123               146
## 5                 1           121           126          123               146
## 6                 1           121           126          123               146
##   teld_exp_standard teld_spokenlang_standard tocs_1_total tocs_2_total
## 1               135                      149           22            6
## 2               135                      149           22            6
## 3               135                      149           22            6
## 4               135                      149           22            6
## 5               135                      149           22            6
## 6               135                      149           22            6
##   tcs_total eprime_condorder_zootask cve_comments comments_tasks handedness_zoo
## 1        25                        1                                         NA
## 2        25                        1                                         NA
## 3        25                        1                                         NA
## 4        25                        1                                         NA
## 5        25                        1                                         NA
## 6        25                        1                                         NA
# Print the unique subject codes
unique_codes<- unique(FULL$Subject)
unique_codes <- as.data.frame(unique_codes)
unique_codes
##    unique_codes
## 1      AE050318
## 2      AH101121
## 3      AK022218
## 4      AK102221
## 5      AL041819
## 6      AN122116
## 7      AS110816
## 8      AT051818
## 9      AW040217
## 10     AW110418
## 11   BW071018T2
## 12     CC031323
## 13   CC102318T2
## 14     CF101019
## 15     CL040218
## 16     CM101322
## 17     CM120919
## 18     DJ052417
## 19     EC041817
## 20     EG030618
## 21     EM100417
## 22     ES031519
## 23     ES032018
## 24     FM032823
## 25     FW121816
## 26     GB012717
## 27   GR091721T2
## 28     HC102117
## 29     HC111621
## 30     HH061919
## 31     HW110822
## 32     JA092118
## 33   JG091119T3
## 34     JJ011018
## 35   JK032119T3
## 36     JM041823
## 37     JS121321
## 38     JT051618
## 39     KA022017
## 40     KE050718
## 41     KT072319
## 42     LB012619
## 43     LB111121
## 44     LG100721
## 45     LO042723
## 46     LT112916
## 47   LW102219T3
## 48     MF101019
## 49     MM040119
## 50   MR091118T2
## 51     MS102319
## 52     NL041119
## 53     OB032423
## 54     OG013016
## 55     OJ032223
## 56     PB021519
## 57     PB031723
## 58     PW030417
## 59     PW041023
## 60     RB041423
## 61     RB101619
## 62     RC102022
## 63     RH100218
## 64   RK040219T3
## 65     RO042723
## 66     RS030518
## 67     RT032219
## 68     RT042523
## 69     SB111121
## 70     SC051023
## 71     SK041519
## 72     SL090418
## 73     SP010219
## 74   ST100121T2
## 75     TA051917
## 76     TE062818
## 77     TS011518
## 78     WB110221
## 79     WF080417
## 80     WH022219
## 81     WK011122
## 82     WS051018
# Making sure the Final FULL dataset has the all subjects coded (initially specified by SUBS - subject number):
subs = 82
(nrow(FULL)/12) == subs # This should give TRUE! 12 rows per subject
## [1] TRUE

MANUALLY ADD TALKER GROUP IS MISSING

# Show the rows where talkergroup_final = 9 or NA :
undefined_talkergroup <- subset(FULL, talkergroup_final == 9 |  is.na(talkergroup_final))
print(unique(undefined_talkergroup$Subject))
## character(0)
# FIND THE UNDEFINED (9) TALKER GROUPS AND MANUALLY MARK THEM AS EITHER 1 or 0 if needed:
# Replace NA values in a specific column based on a condition:
# FULL$talkergroup_final <- ifelse(FULL$Subject == "JA092118", 0, FULL$talkergroup_final)
# FULL$talkergroup_final <- ifelse(FULL$Subject == "LG100721T2", 1, FULL$talkergroup_final)
# FULL$talkergroup_final <- ifelse(FULL$Subject == "LW102219T3", 1, FULL$talkergroup_final)

# Making sure no 9 or NA remained:
any(FULL$talkergroup_final == 9 | is.na(FULL$talkergroup_final))
## [1] FALSE

#PRINT THE NUMBER OF KIDS IN EACH GROUP

# How many kids in each group?
talkergroup_counts <- table(FULL$talkergroup_final)
print(talkergroup_counts)/12
## 
##   0   1 
## 504 480
## 
##  0  1 
## 42 40

ADD THE AVAILABLE LONGITUDINAL CATEGORIES IF AVAILABLE:

# Load DataSet: 
# longitudinal_groups <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/LongitudinalClassifications_9.10.23.csv')

# Merge with the main dataset using SUBJECT
# FULL <- merge(FULL, longitudinal_groups[, c("Subject", "Longitudinal_Group_0Rec_1Per_2Und")], by=c("Subject"), all.x = TRUE)

# Making sure the Final FULL dataset has the all subjects coded (initially specified by SUBS - subject number):
# (nrow(FULL)/4) == subs # This should give TRUE!

# How many kids in each group?
# longtgroup_counts <- table(FULL$Longitudinal_Group_0Rec_1Per_2Und)
# print(longtgroup_counts)/4

LOAD AND MERGE WITH BEHAVIORAL DATA:

Trials with responses faster than 200 ms were eliminated from the analysis, as they were too quick to reflect responding to the current stimulus.

For each participant, the frequency of the following variables was recorded and stored: (a) ‘hits - GO’ (when a Go-stimulus was followed by a response falling between 200 and 2300 ms after stimulus onset), (b) ‘hits - NOGO’ (when a NoGo-stimulus was NOT followed by a response, (c) ‘misses - GO’ (when a Go stimulus was not followed by a response), (d) ‘misses - NOGO - false alarm’ (when a NoGo stimuluswas followed by pressing the response button between 200 and 2300 ms after stimulus onset) (e-f) ‘premature responses - GO and NoGo’ (when the response button was pressed between 0 and 200 ms after stimulus onset). (g) ‘sensitivity’ d’: the z-transformed hit rate minus the z-transformed false alarm rate (Macmillan & Creelman, 2004). A d’ of zero indicates no ability to respond discriminately to the go and no-go stimuli. d = z(H) - z(FA), where z(H) and z(FA) represent the transformation of the hit (correct go trials) and false alarm (commission error) rates to z-scores. The variable d’ represents a measure of the perceptual sensitivity to different stimulus conditions, indicating how well participants can discriminate and appropriately respond to targets and non-targets, thus further inspecting cognitive control [56, 57].

Sensitivity scores, calculated using the formula d=z(H)−z(FA), provide a more detailed and nuanced understanding of the participant’s performance in a Go-NoGo task, taking into account both hits and false alarms and providing insight into the ability to discriminate between Go and NoGo stimuli.

Inhibitory Control and Discrimination Ability:Sensitivity scores provide a more specific measure of the ability to discriminate between Go and NoGo trials.A higher sensitivity score indicates better discrimination ability and better inhibitory control.

Trade-off between Hits and False Alarms: Sensitivity takes into account both correct responses to Go trials (hits) and incorrect responses to NoGo trials (false alarms). It helps you understand the balance or trade-off between the child’s ability to detect Go stimuli and their ability to withhold responses to NoGo stimuli.

Insight into Performance Patterns: A sensitivity score provides a more comprehensive view of performance patterns. For example, a high Go accuracy (90%) might suggest good attention, but a low NoGo accuracy (60%) could indicate a potential issue with inhibitory control. Sensitivity scores help integrate these aspects into a single measure.

Statistical Normalization:By transforming hit and false alarm rates into z-scores, sensitivity scores allow for statistical normalization. This can be particularly useful when comparing performance across different groups or conditions.

For each participant, the frequency of the following variables was automatically recorded and stored: (a) ‘hits’ (when a Go-stimulus was followed by a response falling between 200 and 2300 ms after stimulus onset), (b) ‘misses’ (when a Go- stimulus was not followed by a response), (c) ‘false alarms’ (when a NoGo-stimulus was followed by pressing the response button between 200 and 2300 ms after stimulus onset), and (d) ‘premature responses’ (when the response button was pressed between 0 and 200 ms after stimulus onset).

# Load the file:
accuracy <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/Eprime_01.09.24.csv')
# Take only the relevant variables:
accuracy <-  subset(accuracy, select=c('Name','VisitNumber','ShowStim.ACC','ShowStim.RESP','ShowStim.RT','StimTag'))


# REORGANIZE STIMTAG VARIABLE AS TWO SEPERATE VARIABLES: EMOTION AND CONDITION - EACH WITH TWO LEVELS 
accuracy <- accuracy %>%
  mutate(
    Emotion = str_extract(StimTag, "neu|neg"),
    Condition = str_extract(StimTag, "G|N"),
    Emotion = case_when(
      Emotion == "neu" ~ "Neut",
      Emotion == "neg" ~ "Neg",
      TRUE ~ Emotion
    ),
    Condition = case_when(
      Condition == "G" ~ "Go",
      Condition == "N" ~ "NoGo",
      TRUE ~ Condition
    )
  )

# Convert all empty strings to NA
accuracy <- replace(accuracy, accuracy == "", NA)
# accuracy$ShowStim.RESP <- na_if(accuracy$ShowStim.RESP, "")

# Combine visit number with subject and create a new Subject variable for eprime so that it matches the FULL
accuracy <- accuracy  %>% 
  mutate(Subject = ifelse(VisitNumber != 1, paste0(Name, "T", VisitNumber), Name)) 

# Print unique subjects in FULL
unique_subjects <- unique(FULL$Subject)

# Filter 'accuracy' dataframe by using the Subjects in FULL dataset only
accuracy <- accuracy[accuracy$`Subject` %in% unique_subjects, ]

# Show the unique counts for ShowStim.RESP - which buttons were pressed - number 4 is the proper response button
unique_codes <- unique(accuracy$ShowStim.RESP)
counts <- as.data.frame(table(accuracy$ShowStim.RESP))
counts
##        Var1  Freq
## 1     {ALT}     1
## 2 {CONTROL}     8
## 3   {SHIFT}     1
## 4   {SPACE}     3
## 5         1     5
## 6         2     2
## 7         3    53
## 8         4 19980
## 9         6    11
# Check out the class types for each variable. 
sapply(accuracy, class)
##          Name   VisitNumber  ShowStim.ACC ShowStim.RESP   ShowStim.RT 
##   "character"     "integer"     "integer"   "character"     "integer" 
##       StimTag       Emotion     Condition       Subject 
##   "character"   "character"   "character"   "character"
# For ShowStim.RESP response 4 is a "character", not integer. 
print(class(accuracy$ShowStim.RESP))
## [1] "character"
# Convert character 4 for ShowStim.RESP to integer
# accuracy$ShowStim.RESP <- as.integer(accuracy$ShowStim.RESP)

# Remove the special character trials, and only keep those trials with proper number responses.
unique(accuracy$ShowStim.RESP)
##  [1] "4"         NA          "1"         "{SPACE}"   "3"         "{CONTROL}"
##  [7] "{SHIFT}"   "2"         "{ALT}"     "6"
accuracy <- accuracy[is.na(accuracy$ShowStim.RESP) | accuracy$ShowStim.RESP %in% c(1,2,3,4,5,6) , ]

# Drop the 2 rows with NA in StimTag:
accuracy <- accuracy[!is.na(accuracy$StimTag), ] 

CREATE THE NEW VARIABLES:

# Our max RT is 2023 SO no need to limit it. 

accuracy <- accuracy %>%
  mutate(behavior = case_when(
    (ShowStim.RT >= 200 & ShowStim.RESP == 4) & (StimTag %in% c("negG", "neuG")) ~ "hit_go",
    (ShowStim.RT == 0) & (StimTag %in% c("negG", "neuG")) ~ "miss_go",
    (ShowStim.RT == 0 & (StimTag %in% c("negN", "neuN"))) ~ "hit_nogo",
    (ShowStim.RT >= 200 & (ShowStim.RESP %in% 1:6) & (StimTag %in% c("negN", "neuN"))) ~ "miss_nogo_falsealarm",
    (ShowStim.RT < 200 & ShowStim.RT > 0 & ShowStim.RESP %in% 1:6) & (StimTag %in% c("negG", "neuG")) ~ "premature_go",
    (ShowStim.RT < 200 & ShowStim.RT > 0 & ShowStim.RESP %in% 1:6) & (StimTag %in% c("negN", "neuN")) ~ "premature_nogo",
    TRUE ~ NA_character_   # is a catch-all condition that assigns `NA` to any rows that don't meet any of the previous conditions
  ))

CALCULATE THE PROPORTIONS:

behavioral_measures <- accuracy %>%
  group_by(Subject, Condition, Emotion) %>%
  summarise(
    
  correct_go = sum(behavior == "hit_go", na.rm = TRUE),
  incorrect_go = sum(behavior == "miss_go", na.rm = TRUE),
  premature_go = sum(behavior == "premature_go", na.rm = TRUE),
  total_go = (correct_go + incorrect_go + premature_go),
  accuracy_go_proportion = (correct_go / total_go) *100,
  hit_rate = accuracy_go_proportion/100,
  premature_go_proportion = (premature_go / (correct_go + premature_go))*100,
  
  correct_nogo = sum(behavior == "hit_nogo", na.rm = TRUE),
  incorrect_nogo = sum(behavior == "miss_nogo_falsealarm", na.rm = TRUE), 
  premature_nogo = sum(behavior == "premature_nogo", na.rm = TRUE),
  total_nogo = (correct_nogo + incorrect_nogo + premature_nogo),
  accuracy_nogo_proportion = (correct_nogo / total_nogo) *100, 
  false_alarm_proportion = (incorrect_nogo / total_nogo) *100,
  false_alarm_rate = false_alarm_proportion/100,
  premature_nogo_proportion = ifelse(incorrect_nogo == 0 & premature_nogo == 0, 0, 100 * premature_nogo / max(incorrect_nogo + premature_nogo, 1)), #In this formula, the `ifelse()` function checks if the sum of `incorrect_nogo` and `premature_nogo` is equal to 0. If it is, `premature_nogo_proportion` is set to 0. If the sum is not equal to 0, the original formula is used to calculate `premature_nogo_proportion. By using the max function with the constant 1, the formula guarantees a minimum denominator of 1, preventing division by zero errors. 
  
  # Calculate reaction times: 
  RT_proper_go = mean(ShowStim.RT[behavior == "hit_go"], na.rm = TRUE),
  RT_proper_nogo = mean(ShowStim.RT[behavior == "miss_nogo_falsealarm"], na.rm = TRUE),
  RT_premature_go = mean(ShowStim.RT[behavior == "premature_go"], na.rm = TRUE),
  RT_premature_nogo = mean(ShowStim.RT[behavior == "premature_nogo"], na.rm = TRUE)
  
  # RT_proper_go = mean(ifelse(ShowStim.RT >= 200 & ShowStim.RESP == 4 & (StimTag == 'neuG' | StimTag == 'negG'), ShowStim.RT, NA), na.rm = TRUE),
  # RT_proper_nogo = mean(ifelse(ShowStim.RT >= 200 & ShowStim.RESP %in% 1:6 & (StimTag == 'neuN' | StimTag == 'negN'), ShowStim.RT, NA), na.rm = TRUE),
  # Calculate reaction time for ALL including premature responses
  # RT_all_go = mean(ifelse((StimTag == 'neuG' | StimTag == 'negG') & ShowStim.RT != 0, ShowStim.RT, NA), na.rm = TRUE),
  # RT_all_nogo = mean(ifelse((StimTag == 'neuN' | StimTag == 'negN') & ShowStim.RT != 0, ShowStim.RT, NA), na.rm = TRUE)
  )
## `summarise()` has grouped output by 'Subject', 'Condition'. You can override
## using the `.groups` argument.

See how many kids have missing accuracy data and manually add them:

ADD THIS KID MANUALLY _ ACCURACY TAKEN FROM NETSTATION:

# Get unique Subject IDs in the ZOO dataset
zoo_subjects <- unique(FULL$Subject)

# Get unique Subject IDs in the accuracy_proportions dataset
acc_subjects <- unique(behavioral_measures$Subject)

# Identify Subject IDs present in the ZOO dataset but not in the accuracy_proportions dataset
missing_subjects <- zoo_subjects[!(zoo_subjects %in% acc_subjects)]

# Print the missing Subject IDs
print(missing_subjects)
## [1] "RB101619"
# Create a new data frame with the additional rows
new_rows <- data.frame(Subject = rep("RB101619", 4),
                       Condition = c("Go", "Go", "NoGo", "NoGo"),
                       Emotion = c("Neg", "Neut", "Neg", "Neut"),
                       accuracy_go_proportion = c(99, 99, NA, NA),
                       accuracy_nogo_proportion = c(NA, NA, 42.5, 50),
                       hit_rate = c(.99, .99, NA, NA),
                       false_alarm_rate = c(NA, NA, .425, .5))
                       

# Append the new rows to the existing dataset
behavioral_measures <- rbind(behavioral_measures, new_rows)

# Make sure newly added Subject has data now
behavioral_measures[behavioral_measures$Subject == "RB101619",]
## # A tibble: 4 × 22
## # Groups:   Subject, Condition [2]
##   Subject  Condition Emotion correct_go incorrect_go premature_go total_go
##   <chr>    <chr>     <chr>        <int>        <int>        <int>    <int>
## 1 RB101619 Go        Neg             NA           NA           NA       NA
## 2 RB101619 Go        Neut            NA           NA           NA       NA
## 3 RB101619 NoGo      Neg             NA           NA           NA       NA
## 4 RB101619 NoGo      Neut            NA           NA           NA       NA
## # ℹ 15 more variables: accuracy_go_proportion <dbl>, hit_rate <dbl>,
## #   premature_go_proportion <dbl>, correct_nogo <int>, incorrect_nogo <int>,
## #   premature_nogo <int>, total_nogo <int>, accuracy_nogo_proportion <dbl>,
## #   false_alarm_proportion <dbl>, false_alarm_rate <dbl>,
## #   premature_nogo_proportion <dbl>, RT_proper_go <dbl>, RT_proper_nogo <dbl>,
## #   RT_premature_go <dbl>, RT_premature_nogo <dbl>

Combine accuracy go and accuracy nogo together, premature go and nogo together, and RT go and RT nogo together:

Since ‘accuracy_go’ and ‘accuracy_nogo’ do not overlap (one of them is always NA for a given row), you can use the coalesce function to pick the non-NA value

behavioral_measures <- behavioral_measures %>%
  mutate(
    accuracy = coalesce(accuracy_go_proportion, accuracy_nogo_proportion),
    hit_falsealarm = coalesce(hit_rate, false_alarm_rate),
    premature_responses = coalesce(premature_go_proportion, premature_nogo_proportion),
    RT_proper = coalesce(RT_proper_go, RT_proper_nogo),
    RT_premature = coalesce(RT_premature_go, RT_premature_nogo))

# Calculate z_hit and z_false_alarm and then calculate sensitivity d'- d prime
# Calculate z-scores for hit rate and false alarm rate using the invNorm function

behavioral_measures$hit_rate <- ifelse(behavioral_measures$hit_rate == 1, 0.9999999, behavioral_measures$hit_rate)
behavioral_measures$false_alarm_rate <- ifelse(behavioral_measures$false_alarm_rate == 0, 0.0000001, behavioral_measures$false_alarm_rate)

behavioral_measures <- behavioral_measures %>%
group_by(Subject, Emotion) %>%
  mutate(
    z_hit = qnorm(hit_rate),
    z_false_alarm = qnorm(false_alarm_rate),
    sensitivity = (z_hit[Condition == "Go"] - z_false_alarm[Condition == "NoGo"])
    ) %>%
  ungroup()

# Subset to main variables only
eprime <-  subset(behavioral_measures, select=c('Subject', "Condition", "Emotion", "accuracy", "hit_falsealarm", "premature_responses", "RT_proper", "RT_premature", "sensitivity"))

head(eprime)
## # A tibble: 6 × 9
##   Subject  Condition Emotion accuracy hit_falsealarm premature_responses
##   <chr>    <chr>     <chr>      <dbl>          <dbl>               <dbl>
## 1 AE050318 Go        Neg         75.8          0.758               9.90 
## 2 AE050318 Go        Neut        90            0.9                 0.917
## 3 AE050318 NoGo      Neg         57.5          0.325              23.5  
## 4 AE050318 NoGo      Neut        70            0.25               16.7  
## 5 AH101121 Go        Neg         99.2          0.992               0    
## 6 AH101121 Go        Neut       100            1                   0    
## # ℹ 3 more variables: RT_proper <dbl>, RT_premature <dbl>, sensitivity <dbl>

COMBINE FULL WITH EPRIME

# COMBINE ALL!!
ZOO <- merge(FULL, eprime, by=c("Subject", "Condition", "Emotion"), all.x = TRUE)
head(ZOO)
##    Subject Condition Emotion Trial Type Laterality Electrode MeanAmp_P2
## 1 AE050318        Go     Neg      NegGo       Left        NA  -8.274136
## 2 AE050318        Go     Neg      NegGo    Midline        NA  -8.656789
## 3 AE050318        Go     Neg      NegGo      Right        NA -10.997870
## 4 AE050318        Go    Neut     NeutGo       Left        NA  -8.061495
## 5 AE050318        Go    Neut     NeutGo    Midline        NA -11.009666
## 6 AE050318        Go    Neut     NeutGo      Right        NA -10.983267
##   MeanAmp_N2 MeanAmp_P3 Latency_P2 PeakAmp_P2 Latency_N2 PeakAmp_N2 Latency_P3
## 1  -12.45291  7.0060606        200  -6.982536        368  -17.04337        512
## 2  -17.64050  4.3699974        212  -6.242593        352  -23.05866        612
## 3  -17.06130 -1.0128204        208  -8.224361        352  -20.62662        576
## 4  -12.32419  6.1267276        212  -6.498847        388  -14.78690        516
## 5  -18.54445  4.2534250        220  -8.465257        384  -22.61550        592
## 6  -17.92275 -0.6663896        220  -9.281374        384  -21.63784        488
##   PeakAmp_P3 MeanAmp_N2P2 PeakAmp_N2P2 calculator_age_cve calculator_gender_cve
## 1  9.8622230    -4.178779   -10.060831               38.1                     0
## 2  6.0376027    -8.983711   -16.816070               38.1                     0
## 3  0.8441270    -6.063426   -12.402263               38.1                     0
## 4  8.5292437    -4.262696    -8.288049               38.1                     0
## 5  6.6106956    -7.534787   -14.150243               38.1                     0
## 6  0.3001207    -6.939481   -12.356470               38.1                     0
##   race ethnicity calculator_talkergroup_parent tso_calculated
## 1    2         0                             1            1.9
## 2    2         0                             1            1.9
## 3    2         0                             1            1.9
## 4    2         0                             1            1.9
## 5    2         0                             1            1.9
## 6    2         0                             1            1.9
##   disfluency_sldper100words ssi_total disfluency_sldper100words_final
## 1                        12        23                              12
## 2                        12        23                              12
## 3                        12        23                              12
## 4                        12        23                              12
## 5                        12        23                              12
## 6                        12        23                              12
##   talkergroup_final gfta_standard ppvt_standard evt_standard teld_rec_standard
## 1                 1           121           126          123               146
## 2                 1           121           126          123               146
## 3                 1           121           126          123               146
## 4                 1           121           126          123               146
## 5                 1           121           126          123               146
## 6                 1           121           126          123               146
##   teld_exp_standard teld_spokenlang_standard tocs_1_total tocs_2_total
## 1               135                      149           22            6
## 2               135                      149           22            6
## 3               135                      149           22            6
## 4               135                      149           22            6
## 5               135                      149           22            6
## 6               135                      149           22            6
##   tcs_total eprime_condorder_zootask cve_comments comments_tasks handedness_zoo
## 1        25                        1                                         NA
## 2        25                        1                                         NA
## 3        25                        1                                         NA
## 4        25                        1                                         NA
## 5        25                        1                                         NA
## 6        25                        1                                         NA
##   accuracy hit_falsealarm premature_responses RT_proper RT_premature
## 1 75.83333      0.7583333           9.9009901   774.044         87.1
## 2 75.83333      0.7583333           9.9009901   774.044         87.1
## 3 75.83333      0.7583333           9.9009901   774.044         87.1
## 4 90.00000      0.9000000           0.9174312   802.213        151.0
## 5 90.00000      0.9000000           0.9174312   802.213        151.0
## 6 90.00000      0.9000000           0.9174312   802.213        151.0
##   sensitivity
## 1    1.154714
## 2    1.154714
## 3    1.154714
## 4    1.956041
## 5    1.956041
## 6    1.956041

LOAD and merge with trail numbers DF

# Load the file:
# trialnum <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/Zoo_trialnumber_used_10.03.23.csv')

# convert the dataset to long format:
# id.vars argument specifies which columns in the original data frame should remain as they are without being transformed.
# measure.vars argument specifies which columns in the original data frame should be melted
# The variable.name argument specifies the name of the new column that will store the variable names from the measure.vars
# The value.name argument specifies the name of the new column that will store the values from the measure.vars.
# trialnum_long <- melt(trialnum, id.vars = c("Subject"),
#                      measure.vars = c("NeutNoGo","NeutGo",
#                                       "NegNoGo", "NegGo"), 
#                      variable.name = "StimTag", 
#                      value.name ="TrialNum")

# ZOO <- merge(ZOO, trialnum_long, by=c("Subject", "Condition", "Emotion"), all.x = TRUE)

#COMPUTE AND ADD BRIEF SCORES

INHIBIT: This scale assesses inhibitory control and impulsivity. Higher scores indicate greater difficulty in resisting impulses and stopping one’s behavior at the appropriate time.

SHIFT: The Shift scale assesses the ability to move freely from one situation, activity, or aspect of a problem to another as the circumstances demand. It includes aspects such as making transitions, tolerating change, problem-solving flexibly, switching attention, and changing focus.The ability to shift attention and adapt to changing demands is relevant to tasks like the Go NoGo task, and a correlation may be observed between the Shift scale and behavioral accuracy.

emotionalCntrl: The Emotional Control scale measures the impact of executive function problems on emotional expression and assesses a child’s ability to modulate or control emotional responses.Emotional control is relevant to behavioral accuracy, especially in tasks that may evoke emotional responses. This scale could also be relevant to N2 and P3 components, as these components are associated with cognitive and attentional processes.

# Load the file:
brief <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/Brief.csv')

# Drop all rows that has an NA for the first three items (assuming the rest is empty too)
brief <- brief[complete.cases(brief[, c("brief1", "brief2", "brief3")]), ]

# replace individual NA values with 2 (the average score)
brief <- replace(brief, is.na(brief), 2)


# BRIEF PRESCHOOL AGED:

brief <- brief %>%
  mutate(inhibit = brief3 + brief8 + brief13 + brief18 + brief23 + brief28 + brief33 + brief38 + brief43 + brief48 + brief52 + brief54 + brief56 + brief58 + brief60 + brief62,
         shift = brief5 + brief10 + brief15 + brief20 + brief25 + brief30 + brief35 + brief40 + brief45 + brief50,
         emotionalCntrl = brief1 + brief6 + brief11 + brief16 + brief21 + brief26 + brief31 + brief36 + brief41 + brief46,
         workingMemory = brief2 + brief7 + brief12 + brief17 + brief22 + brief27 + brief32 + brief37 + brief42 + brief47 + brief51 + brief53 + brief55 + brief57 + brief59 + brief61 + brief63,
         planOrganize = brief4 + brief9 + brief14 + brief19 + brief24 + brief29 + brief34 + brief39 + brief44 + brief49,
         InhibitorySelfControlIndex_ISCI  = inhibit + emotionalCntrl,
         FlexibilityIndex_FI  = shift + emotionalCntrl,
         BehavioralRegulationIndex_BRI = inhibit + shift + emotionalCntrl,
         MetacognitionIndex_MI  = workingMemory + planOrganize,
         GlobalExecutiveComposite_GEC = BehavioralRegulationIndex_BRI + MetacognitionIndex_MI) %>%
  dplyr::select(part_id_status, redcap_event_name, inhibit:GlobalExecutiveComposite_GEC) # with select specify it is dplyr, because it conflicts with other packages. 


# Create the Subject code with time stamp
brief <- brief %>% 
  mutate(
    Subject = case_when(grepl("t1_arm_1", redcap_event_name) ~ paste0(part_id_status),
                        grepl("t2_arm_1", redcap_event_name) ~ paste0(part_id_status, "T2"),
                        grepl("t3_arm_1", redcap_event_name) ~ paste0(part_id_status, "T3"),
                        grepl("t4_arm_1", redcap_event_name) ~ paste0(part_id_status, "T3"),
                        TRUE ~ NA_character_)
    ) 


# BRIEF SCHOOL AGED:

# Load the file:
brief_s <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/Brief_s.csv')

# Drop all rows that has an NA for the first three items (assuming the rest is empty too)
brief_s <- brief_s[complete.cases(brief_s[, c("brief1s", "brief2s", "brief3s")]), ]

# replace individual NA values with 2 (the average score)
brief_s <- replace(brief_s, is.na(brief_s), 2)


brief_s <- brief_s %>%
  mutate(inhibit = brief38s + brief41s + brief43s + brief44s + brief49s + brief54s + brief55_s + brief56s + brief59s + brief65s,
         shift = brief5s + brief6s + brief8s + brief12s + brief13s + brief23s + brief30s + brief39s,
         emotionalCntrl = brief1s + brief7s + brief20s + brief25s + brief26s + brief45s + brief50s + brief62s + brief64s + brief70s,
         initiate = brief3s + brief10s + brief16s + brief47s + brief48s + brief61s + brief66s + brief71s,
         workingMemory = briefs11s + brief15s + brief18s + brief22s + brief28s + brief35s + brief36s + brief40s + brief46s + brief51s + brief53s + brief58s,
         planOrganize = brief4s + brief9s + brief14s + brief19s + brief24s + brief29s + brief34s + brief39s + brief44s + brief49s,
         orgofMaterials = brief4s + brief29s + brief67s + brief68s + brief69s + brief72s,
         monitor = brief14s + brief21s + brief31s + brief34s + brief42s + brief52s + brief60s + brief63_s,
         # composite scales:
         BehavioralRegulationIndex_BRI = inhibit + shift + emotionalCntrl,
         MetacognitionIndex_MI = initiate + workingMemory + planOrganize + orgofMaterials + monitor,
         GlobalExecutiveComposite_GEC = BehavioralRegulationIndex_BRI + MetacognitionIndex_MI)  %>%
  dplyr::select(part_id_status, redcap_event_name, inhibit:GlobalExecutiveComposite_GEC)

# Create the Subject code with time stamp
brief_s <- brief_s %>% 
  mutate(
    Subject = case_when(grepl("t1_arm_1", redcap_event_name) ~ paste0(part_id_status),
                        grepl("t2_arm_1", redcap_event_name) ~ paste0(part_id_status, "T2"),
                        grepl("t3_arm_1", redcap_event_name) ~ paste0(part_id_status, "T3"),
                        grepl("t4_arm_1", redcap_event_name) ~ paste0(part_id_status, "T4"),
                        TRUE ~ NA_character_)
    ) 


# Merge them together
# In the by section list all common variable names: 
brief_merged <- merge(brief, brief_s, by = c("Subject", "part_id_status", "redcap_event_name", "inhibit", "shift", "emotionalCntrl", "workingMemory", "planOrganize", "BehavioralRegulationIndex_BRI", "MetacognitionIndex_MI", "GlobalExecutiveComposite_GEC", "inhibit", "shift", "inhibit", "shift"), all = TRUE)


# Merge with ZOO
ZOO <- merge(ZOO, brief_merged, by=c("Subject"), all.x = TRUE)

# 2 KIDS DATA IS MISSING
# unique(ZOO$Subject[is.na(ZOO$inhibit)])
# [1] "GB012717" "OG013016"

#COMPUTE AND ADD CBQ SCORES

# Load the file:
cbq <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CBQ.csv')

# Calculate percentage of NA values in each row
# na_percent <- rowSums(is.na(cbq))/ncol(cbq)
# Subset rows with less than 70% NA values
# cbq <- cbq[na_percent < 0.7, ]

# Define a function to calculate r values
subtract_eight <- function(x) {
8 - x
}
# Apply the function to columns cbq0001 through cbq0195
cbq <- cbq %>%
mutate(across(matches("^cbq0[01][0-9][0-9]$"), subtract_eight, .names = "{col}r"))

#Calculate the sub scales - create new variables
cbq <- cbq %>%
mutate(
  act = rowMeans(dplyr::select(., cbq0001, cbq0025, cbq0041r, cbq0048, cbq0088r, cbq0102r, cbq0123r, cbq0126r, cbq0145r, cbq0152, cbq0172, cbq0187, cbq0192r), na.rm = TRUE),
  fru = rowMeans(dplyr::select(., cbq0002, cbq0019r, cbq0034, cbq0062, cbq0073, cbq0078, cbq0120r, cbq0128, cbq0140, cbq0156r, cbq0173, cbq0181, cbq0193), na.rm = TRUE),
  app = rowMeans(dplyr::select(., cbq0010, cbq0024, cbq0035, cbq0069, cbq0082, cbq0096, cbq0117, cbq0131r, cbq0148, cbq0166, cbq0175r, cbq0188r, cbq0191r), na.rm = TRUE),
  attfo = rowMeans(dplyr::select(., cbq0016, cbq0038r, cbq0047r, cbq0125, cbq0144, cbq0160, cbq0171r, cbq0186, cbq0195r), na.rm = TRUE),
  attshi = rowMeans(dplyr::select(., cbq0006r, cbq0029, cbq0095r, cbq0180, cbq0184r), na.rm = TRUE),
  dis = rowMeans(dplyr::select(., cbq0005r, cbq0021, cbq0061, cbq0087, cbq0097, cbq0101r, cbq0115, cbq0132, cbq0141, cbq0157, cbq0178, cbq0190r), na.rm = TRUE),
  sth = rowMeans(dplyr::select(., cbq0014r, cbq0027, cbq0042, cbq0053r, cbq0068r, cbq0085, cbq0092, cbq0103, cbq0118, cbq0134, cbq0150r, cbq0167r, cbq0177), na.rm = TRUE),
  fea = rowMeans(dplyr::select(., cbq0015r, cbq0040, cbq0050, cbq0058r, cbq0070r, cbq0080, cbq0091, cbq0130, cbq0138r, cbq0161r, cbq0176r, cbq0189), na.rm = TRUE),
  hip = rowMeans(dplyr::select(., cbq0008, cbq0022, cbq0030r, cbq0051r, cbq0060r, cbq0067, cbq0077, cbq0100, cbq0107, cbq0124, cbq0139, cbq0159r, cbq0182), na.rm = TRUE),
  imp = rowMeans(dplyr::select(., cbq0013, cbq0026, cbq0046, cbq0059, cbq0071r, cbq0079r, cbq0090r, cbq0104, cbq0114, cbq0137r, cbq0155, cbq0169r, cbq0183r), na.rm = TRUE),
  inh = rowMeans(dplyr::select(., cbq0004, cbq0020, cbq0032r, cbq0063, cbq0075, cbq0093r, cbq0108r, cbq0116, cbq0136, cbq0147, cbq0162r, cbq0168, cbq0185), na.rm = TRUE),
  lip = rowMeans(dplyr::select(., cbq0012r, cbq0036, cbq0054, cbq0066r, cbq0076,cbq0111r, cbq0113, cbq0133, cbq0146, cbq0151,cbq0164, cbq0174), na.rm = TRUE),
  per = rowMeans(dplyr::select(., cbq0009, cbq0028r, cbq0031, cbq0052, cbq0065,cbq0084r, cbq0098, cbq0105, cbq0122r, cbq0142r,cbq0154, cbq0170r), na.rm = TRUE),
  sad = rowMeans(dplyr::select(., cbq0018, cbq0039, cbq0044, cbq0055, cbq0064,cbq0072r, cbq0081, cbq0094, cbq0109r, cbq0112r,cbq0127, cbq0149r), na.rm = TRUE),
  shy = rowMeans(dplyr::select(., cbq0007, cbq0017r, cbq0023r, cbq0037,cbq0045r, cbq0057r, cbq0074, cbq0089, cbq0106,cbq0119r, cbq0129r, cbq0143, cbq0158r), na.rm = TRUE),
  smi =rowMeans(dplyr::select(., cbq0011, cbq0043r, cbq0056, cbq0083r,cbq0099r, cbq0110, cbq0121r, cbq0135r, cbq0152,cbq0163, cbq0165r, cbq0179, cbq0194), na.rm = TRUE)
)


cbq <- cbq %>%
  mutate(shy_r=(8-shy)) %>%
  mutate(sth_r=(8-sth)) %>%
  rename(activity_level = act) %>%
  rename(anger_frustration = fru) %>%
  rename(approach = app) %>%
  rename(attention_focus = attfo) %>%
  rename(attention_shift = attshi) %>%
  rename(discomfort = dis) %>%
  rename(soothability = sth) %>%
  rename(fear = fea) %>%
  rename(hi_intense_pleas = hip) %>%
  rename(impulsivity = imp) %>%
  rename(inhibit_control = inh) %>%
  rename(lo_instense_pleas = lip) %>%
  rename(percept_sensitive = per) %>%
  rename(sadness = sad) %>%
  rename(shyness = shy) %>%
  rename(smiling_laughter = smi)

           
cbq <- cbq %>%
  mutate(surgency = rowMeans(dplyr::select(.,  activity_level,hi_intense_pleas, impulsivity, shy_r)),
         effortful_con = rowMeans(dplyr::select(.,  attention_focus, inhibit_control,lo_instense_pleas, percept_sensitive)),
         neg_affect = rowMeans(dplyr::select(.,  anger_frustration, discomfort,fear, sadness, sth_r))) %>% 
  dplyr::select(part_id_status, redcap_event_name, activity_level:neg_affect)


# Create the Subject code with time stamp
cbq <- cbq %>% 
  mutate(
    Subject = case_when(grepl("t1_arm_1", redcap_event_name) ~ paste0(part_id_status),
                        grepl("t2_arm_1", redcap_event_name) ~ paste0(part_id_status, "T2"),
                        grepl("t3_arm_1", redcap_event_name) ~ paste0(part_id_status, "T3"),
                        TRUE ~ NA_character_)
    )

# Merge with ZOO
ZOO <- merge(ZOO, cbq, by=c("Subject"), all.x = TRUE)

WRITE THE FINAL DATAFRAME INTO CSV AND SAVE TO LOCAL DRIVE:

# WRITE THE FINAL DATAFRAME INTO CSV AND SAVE TO LOCAL DRIVE:
write.csv(ZOO, "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/ZOO.csv")
head(ZOO)
##    Subject Condition Emotion Trial Type Laterality Electrode  MeanAmp_P2
## 1 AE050318        Go     Neg      NegGo    Midline        NA  -8.6567894
## 2 AE050318        Go    Neut     NeutGo       Left        NA  -8.0614951
## 3 AE050318        Go     Neg      NegGo       Left        NA  -8.2741357
## 4 AE050318      NoGo     Neg    NegNoGo      Right        NA   3.0738593
## 5 AE050318        Go     Neg      NegGo      Right        NA -10.9978696
## 6 AE050318      NoGo     Neg    NegNoGo    Midline        NA   0.1368089
##    MeanAmp_N2 MeanAmp_P3 Latency_P2 PeakAmp_P2 Latency_N2 PeakAmp_N2 Latency_P3
## 1 -17.6405009   4.369997        212  -6.242593        352 -23.058663        612
## 2 -12.3241910   6.126728        212  -6.498847        388 -14.786896        516
## 3 -12.4529149   7.006061        200  -6.982536        368 -17.043367        512
## 4  -0.6563204   0.449258        232   5.512485        400  -3.018455        508
## 5 -17.0612959  -1.012820        208  -8.224361        352 -20.626624        576
## 6  -2.7636031   3.345500        252   3.300071        452  -5.257907        600
##   PeakAmp_P3 MeanAmp_N2P2 PeakAmp_N2P2 calculator_age_cve calculator_gender_cve
## 1   6.037603    -8.983711   -16.816070               38.1                     0
## 2   8.529244    -4.262696    -8.288049               38.1                     0
## 3   9.862223    -4.178779   -10.060831               38.1                     0
## 4   3.116096    -3.730180    -8.530940               38.1                     0
## 5   0.844127    -6.063426   -12.402263               38.1                     0
## 6   6.301873    -2.900412    -8.557978               38.1                     0
##   race ethnicity calculator_talkergroup_parent tso_calculated
## 1    2         0                             1            1.9
## 2    2         0                             1            1.9
## 3    2         0                             1            1.9
## 4    2         0                             1            1.9
## 5    2         0                             1            1.9
## 6    2         0                             1            1.9
##   disfluency_sldper100words ssi_total disfluency_sldper100words_final
## 1                        12        23                              12
## 2                        12        23                              12
## 3                        12        23                              12
## 4                        12        23                              12
## 5                        12        23                              12
## 6                        12        23                              12
##   talkergroup_final gfta_standard ppvt_standard evt_standard teld_rec_standard
## 1                 1           121           126          123               146
## 2                 1           121           126          123               146
## 3                 1           121           126          123               146
## 4                 1           121           126          123               146
## 5                 1           121           126          123               146
## 6                 1           121           126          123               146
##   teld_exp_standard teld_spokenlang_standard tocs_1_total tocs_2_total
## 1               135                      149           22            6
## 2               135                      149           22            6
## 3               135                      149           22            6
## 4               135                      149           22            6
## 5               135                      149           22            6
## 6               135                      149           22            6
##   tcs_total eprime_condorder_zootask cve_comments comments_tasks handedness_zoo
## 1        25                        1                                         NA
## 2        25                        1                                         NA
## 3        25                        1                                         NA
## 4        25                        1                                         NA
## 5        25                        1                                         NA
## 6        25                        1                                         NA
##   accuracy hit_falsealarm premature_responses RT_proper RT_premature
## 1 75.83333      0.7583333           9.9009901   774.044        87.10
## 2 90.00000      0.9000000           0.9174312   802.213       151.00
## 3 75.83333      0.7583333           9.9009901   774.044        87.10
## 4 57.50000      0.3250000          23.5294118   566.000       111.75
## 5 75.83333      0.7583333           9.9009901   774.044        87.10
## 6 57.50000      0.3250000          23.5294118   566.000       111.75
##   sensitivity part_id_status.x redcap_event_name.x inhibit shift emotionalCntrl
## 1    1.154714         AE050318            t1_arm_1      36    15             13
## 2    1.956041         AE050318            t1_arm_1      36    15             13
## 3    1.154714         AE050318            t1_arm_1      36    15             13
## 4    1.154714         AE050318            t1_arm_1      36    15             13
## 5    1.154714         AE050318            t1_arm_1      36    15             13
## 6    1.154714         AE050318            t1_arm_1      36    15             13
##   workingMemory planOrganize BehavioralRegulationIndex_BRI
## 1            37           20                            64
## 2            37           20                            64
## 3            37           20                            64
## 4            37           20                            64
## 5            37           20                            64
## 6            37           20                            64
##   MetacognitionIndex_MI GlobalExecutiveComposite_GEC
## 1                    57                          121
## 2                    57                          121
## 3                    57                          121
## 4                    57                          121
## 5                    57                          121
## 6                    57                          121
##   InhibitorySelfControlIndex_ISCI FlexibilityIndex_FI initiate orgofMaterials
## 1                              49                  28       NA             NA
## 2                              49                  28       NA             NA
## 3                              49                  28       NA             NA
## 4                              49                  28       NA             NA
## 5                              49                  28       NA             NA
## 6                              49                  28       NA             NA
##   monitor part_id_status.y redcap_event_name.y activity_level anger_frustration
## 1      NA         AE050318            t1_arm_1       5.307692          3.923077
## 2      NA         AE050318            t1_arm_1       5.307692          3.923077
## 3      NA         AE050318            t1_arm_1       5.307692          3.923077
## 4      NA         AE050318            t1_arm_1       5.307692          3.923077
## 5      NA         AE050318            t1_arm_1       5.307692          3.923077
## 6      NA         AE050318            t1_arm_1       5.307692          3.923077
##   approach attention_focus attention_shift discomfort soothability     fear
## 1 3.846154        2.666667             5.2   3.416667     5.307692 3.666667
## 2 3.846154        2.666667             5.2   3.416667     5.307692 3.666667
## 3 3.846154        2.666667             5.2   3.416667     5.307692 3.666667
## 4 3.846154        2.666667             5.2   3.416667     5.307692 3.666667
## 5 3.846154        2.666667             5.2   3.416667     5.307692 3.666667
## 6 3.846154        2.666667             5.2   3.416667     5.307692 3.666667
##   hi_intense_pleas impulsivity inhibit_control lo_instense_pleas
## 1         5.307692    4.307692               4          5.166667
## 2         5.307692    4.307692               4          5.166667
## 3         5.307692    4.307692               4          5.166667
## 4         5.307692    4.307692               4          5.166667
## 5         5.307692    4.307692               4          5.166667
## 6         5.307692    4.307692               4          5.166667
##   percept_sensitive  sadness  shyness smiling_laughter    shy_r    sth_r
## 1          4.666667 2.916667 3.692308         6.307692 4.307692 2.692308
## 2          4.666667 2.916667 3.692308         6.307692 4.307692 2.692308
## 3          4.666667 2.916667 3.692308         6.307692 4.307692 2.692308
## 4          4.666667 2.916667 3.692308         6.307692 4.307692 2.692308
## 5          4.666667 2.916667 3.692308         6.307692 4.307692 2.692308
## 6          4.666667 2.916667 3.692308         6.307692 4.307692 2.692308
##   surgency effortful_con neg_affect
## 1 4.807692         4.125   3.323077
## 2 4.807692         4.125   3.323077
## 3 4.807692         4.125   3.323077
## 4 4.807692         4.125   3.323077
## 5 4.807692         4.125   3.323077
## 6 4.807692         4.125   3.323077

REMOVE THESE KIDS BASED ON LOWER ACCURACY + NEGATIVE EEG COMMENTS:

ZOO_bad_subjects <- ZOO %>%
  group_by(Subject) %>%
  filter(all(accuracy[`Trial Type` == "NeutNoGo"] <= 40) |
         all(accuracy[`Trial Type` == "NegNoGo"] <= 40) |
         all(accuracy[`Trial Type` == "NeutGo"] <= 70) |
         all(accuracy[`Trial Type` == "NegGo"] <= 70))

bad_subjects = unique(ZOO_bad_subjects$Subject)
bad_subjects
## [1] "EG030618" "HH061919" "JS121321" "NL041119" "RT032219" "WS051018"
# READ THE COMMENTS FOR THOSE LOW ACCURACY KIDS:
ZOO_filtered <- dplyr::filter(ZOO, Subject %in% bad_subjects)
ZOO_filtered <-  subset(ZOO_filtered, select=c('Subject','talkergroup_final','Trial Type','accuracy', 'calculator_age_cve','RT_proper', 'cve_comments','comments_tasks','handedness_zoo'))
ZOO_filtered <- ZOO_filtered %>% distinct() # Use distinct function to remove duplicates
print(ZOO_filtered)
##     Subject talkergroup_final Trial Type accuracy calculator_age_cve RT_proper
## 1  EG030618                 1      NegGo 73.94958               48.8  830.8182
## 2  EG030618                 1     NeutGo 84.16667               48.8  854.0990
## 3  EG030618                 1    NegNoGo 30.00000               48.8  894.8333
## 4  EG030618                 1   NeutNoGo 72.50000               48.8  803.7273
## 5  HH061919                 1     NeutGo 95.00000               76.4  556.8509
## 6  HH061919                 1      NegGo 80.50847               76.4  655.8947
## 7  HH061919                 1    NegNoGo 60.00000               76.4  666.4000
## 8  HH061919                 1   NeutNoGo 35.00000               76.4  467.3200
## 9  JS121321                 1      NegGo 69.49153               41.5  963.2683
## 10 JS121321                 1   NeutNoGo 72.50000               41.5 1088.6250
## 11 JS121321                 1    NegNoGo 85.00000               41.5 1112.6667
## 12 JS121321                 1     NeutGo 84.16667               41.5  873.1287
## 13 NL041119                 1    NegNoGo 75.00000               41.8  693.3000
## 14 NL041119                 1   NeutNoGo 45.00000               41.8  891.6818
## 15 NL041119                 1      NegGo 80.83333               41.8  920.0206
## 16 NL041119                 1     NeutGo 62.50000               41.8  844.6267
## 17 RT032219                 0      NegGo 78.33333               38.0  936.8191
## 18 RT032219                 0     NeutGo 64.16667               38.0  913.0519
## 19 RT032219                 0    NegNoGo 62.50000               38.0  622.0714
## 20 RT032219                 0   NeutNoGo 45.00000               38.0  958.1905
## 21 WS051018                 1      NegGo 85.00000               77.3  537.2451
## 22 WS051018                 1     NeutGo 94.95798               77.3  556.3451
## 23 WS051018                 1    NegNoGo 40.00000               77.3  412.8571
## 24 WS051018                 1   NeutNoGo 52.50000               77.3  491.0000
##                                                                                                                                                                                                                         cve_comments
## 1                                                                                                                                                                          No emotional regulation, no rhythm task, no reaction time
## 2                                                                                                                                                                          No emotional regulation, no rhythm task, no reaction time
## 3                                                                                                                                                                          No emotional regulation, no rhythm task, no reaction time
## 4                                                                                                                                                                          No emotional regulation, no rhythm task, no reaction time
## 5                                                                                                                                                                          Performed TELD B to see if he scored above our cut off.  
## 6                                                                                                                                                                          Performed TELD B to see if he scored above our cut off.  
## 7                                                                                                                                                                          Performed TELD B to see if he scored above our cut off.  
## 8                                                                                                                                                                          Performed TELD B to see if he scored above our cut off.  
## 9                                                                                                                                                                Another speech sample was taken since he was borderline last time. 
## 10                                                                                                                                                               Another speech sample was taken since he was borderline last time. 
## 11                                                                                                                                                               Another speech sample was taken since he was borderline last time. 
## 12                                                                                                                                                               Another speech sample was taken since he was borderline last time. 
## 13                                                                                                                                                                                                                                  
## 14                                                                                                                                                                                                                                  
## 15                                                                                                                                                                                                                                  
## 16                                                                                                                                                                                                                                  
## 17                                                                                                                                                                                                                                  
## 18                                                                                                                                                                                                                                  
## 19                                                                                                                                                                                                                                  
## 20                                                                                                                                                                                                                                  
## 21 The child had difficulty attending to most tasks including the NIH toolbox. Fidgeting and changing hands to response frequently. He had hard time sitting still. With the EEg cap he was restless and moving frequently as well. 
## 22 The child had difficulty attending to most tasks including the NIH toolbox. Fidgeting and changing hands to response frequently. He had hard time sitting still. With the EEg cap he was restless and moving frequently as well. 
## 23 The child had difficulty attending to most tasks including the NIH toolbox. Fidgeting and changing hands to response frequently. He had hard time sitting still. With the EEg cap he was restless and moving frequently as well. 
## 24 The child had difficulty attending to most tasks including the NIH toolbox. Fidgeting and changing hands to response frequently. He had hard time sitting still. With the EEg cap he was restless and moving frequently as well. 
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              comments_tasks
## 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
## 3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
## 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
## 5  During the Zoo Task, he required a lot of reminders to not push the button for the helpers. During the second block (affective) of the Zoo Task, he said he was very tired and refused to push the button for about 10-20 trials- required a lot of redirection to keep going. During the Reactivity Task, the second block(unpleasant) he moved a lot and talked a lot- required a lot of redirection to keep going and the skin conductance electrode fell off for part of that block.   RESTING STATE and ZOO TASK are in one file.  
## 6  During the Zoo Task, he required a lot of reminders to not push the button for the helpers. During the second block (affective) of the Zoo Task, he said he was very tired and refused to push the button for about 10-20 trials- required a lot of redirection to keep going. During the Reactivity Task, the second block(unpleasant) he moved a lot and talked a lot- required a lot of redirection to keep going and the skin conductance electrode fell off for part of that block.   RESTING STATE and ZOO TASK are in one file.  
## 7  During the Zoo Task, he required a lot of reminders to not push the button for the helpers. During the second block (affective) of the Zoo Task, he said he was very tired and refused to push the button for about 10-20 trials- required a lot of redirection to keep going. During the Reactivity Task, the second block(unpleasant) he moved a lot and talked a lot- required a lot of redirection to keep going and the skin conductance electrode fell off for part of that block.   RESTING STATE and ZOO TASK are in one file.  
## 8  During the Zoo Task, he required a lot of reminders to not push the button for the helpers. During the second block (affective) of the Zoo Task, he said he was very tired and refused to push the button for about 10-20 trials- required a lot of redirection to keep going. During the Reactivity Task, the second block(unpleasant) he moved a lot and talked a lot- required a lot of redirection to keep going and the skin conductance electrode fell off for part of that block.   RESTING STATE and ZOO TASK are in one file.  
## 9                                                                                                                                                                                                                                                                                                                                                                                                                         It took him a while to get the task during zoo but he got it towards the 2nd quarter. Good attention in general. 
## 10                                                                                                                                                                                                                                                                                                                                                                                                                        It took him a while to get the task during zoo but he got it towards the 2nd quarter. Good attention in general. 
## 11                                                                                                                                                                                                                                                                                                                                                                                                                        It took him a while to get the task during zoo but he got it towards the 2nd quarter. Good attention in general. 
## 12                                                                                                                                                                                                                                                                                                                                                                                                                        It took him a while to get the task during zoo but he got it towards the 2nd quarter. Good attention in general. 
## 13                                                                                                                                                                                                                                                                                                                                                                                              DO NOT use Reactivity data. Child was rarely looking at the screen.  She was talking to mom and putting her head down to rest on the table.
## 14                                                                                                                                                                                                                                                                                                                                                                                              DO NOT use Reactivity data. Child was rarely looking at the screen.  She was talking to mom and putting her head down to rest on the table.
## 15                                                                                                                                                                                                                                                                                                                                                                                              DO NOT use Reactivity data. Child was rarely looking at the screen.  She was talking to mom and putting her head down to rest on the table.
## 16                                                                                                                                                                                                                                                                                                                                                                                              DO NOT use Reactivity data. Child was rarely looking at the screen.  She was talking to mom and putting her head down to rest on the table.
## 17                                                                                                                                                                                                                                                                    RESTING: mom was talking throughout to help him attend (according to her)    ZOO TASK: Concentration and motivation varied    REACTIVITY: Task was started before Acqknowledge was running. Consequently, the physio for the first several pictures was not recorded.
## 18                                                                                                                                                                                                                                                                    RESTING: mom was talking throughout to help him attend (according to her)    ZOO TASK: Concentration and motivation varied    REACTIVITY: Task was started before Acqknowledge was running. Consequently, the physio for the first several pictures was not recorded.
## 19                                                                                                                                                                                                                                                                    RESTING: mom was talking throughout to help him attend (according to her)    ZOO TASK: Concentration and motivation varied    REACTIVITY: Task was started before Acqknowledge was running. Consequently, the physio for the first several pictures was not recorded.
## 20                                                                                                                                                                                                                                                                    RESTING: mom was talking throughout to help him attend (according to her)    ZOO TASK: Concentration and motivation varied    REACTIVITY: Task was started before Acqknowledge was running. Consequently, the physio for the first several pictures was not recorded.
## 21                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
## 22                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
## 23                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
## 24                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
##    handedness_zoo
## 1              NA
## 2              NA
## 3              NA
## 4              NA
## 5               1
## 6               1
## 7               1
## 8               1
## 9               1
## 10              1
## 11              1
## 12              1
## 13              1
## 14              1
## 15              1
## 16              1
## 17              1
## 18              1
## 19              1
## 20              1
## 21             NA
## 22             NA
## 23             NA
## 24             NA

REMOVE THESE KIDS with <30% accuracy in Nogo, and <60% in Go.

subjects_to_remove <- c("EG030618","HH061919","WS051018")      

ZOO_good <- ZOO
ZOO_good <- dplyr::filter(ZOO_good, !Subject %in% subjects_to_remove)

write.csv(ZOO_good, "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/ZOO_good.csv")

DRAW N2P2 and P3 WAVEFORM GRAPHS YOURSELF

# Calculate the average of specific columns (as specificed by these electrode numbers below) across all rows and create a new column with these averages
# Combine all conditions together into a single dataframe
# This step is necessary to reverse the unclass() function we have used above. 
# We need to create subsets from the original combo dataset first ADN THEN unclass before using m.measures()
combo_new <- rbind.data.frame(neg_go, neg_nogo, neut_go, neut_nogo) 
combo_old <- rbind.data.frame(neg_go_old, neg_nogo_old, neut_go_old, neut_nogo_old) 

# Add the "talkergroup_final" column from the "FULL" dataset to the "combo" dataset based on the common "Subject" column
combo_with_group_new <- combo_new %>%
  mutate(talkergroup_final = FULL$talkergroup_final[match(Subject, FULL$Subject)])

combo_with_group_old <- combo_old %>%
  mutate(talkergroup_final = FULL$talkergroup_final[match(Subject, FULL$Subject)])

# REMOVE THESE THREE KIDS FROM THE DATASET:
combo_with_group_new <- dplyr::filter(combo_with_group_new, Subject!="EG030618", Subject!="HH061919", Subject!="WS051018")


# CREATE MEAN N2P2 AND P3 VALUES FOR EACH ROW (TIMEPOINT) USING THE SPECIFIED ELECTRODE NUMBERS 

newnet_rawdata <- combo_with_group_new  %>%
    mutate(
    N2P2_waveform = rowMeans(dplyr::select(., all_of(FCz_newnets))),
    P3_waveform = rowMeans(dplyr::select(., all_of(Pz_newnets)))
    )

oldnet_rawdata <- combo_with_group_old  %>%
    mutate(
    N2P2_waveform = rowMeans(dplyr::select(., all_of(FCz_oldnets))),
    P3_waveform = rowMeans(dplyr::select(., all_of(Pz_oldnets)))
    )

# Combine old and new net data together:
rawdata <- full_join(newnet_rawdata, oldnet_rawdata)
## Joining with `by = join_by(Subject, Stimulus, Time, V1, V2, V3, V4, V5, V6, V7,
## V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23,
## V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39,
## V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55,
## V56, V57, V58, V59, V60, V61, V62, V63, V64, V65, V66, V67, V68, V69, V70, V71,
## V72, V73, V74, V75, V76, V77, V78, V79, V80, V81, V82, V83, V84, V85, V86, V87,
## V88, V89, V90, V91, V92, V93, V94, V95, V96, V97, V98, V99, V100, V101, V102,
## V103, V104, V105, V106, V107, V108, V109, V110, V111, V112, V113, V114, V115,
## V116, V117, V118, V119, V120, V121, V122, V123, V124, V125, V126, V127, V128,
## V129, talkergroup_final, N2P2_waveform, P3_waveform)`
# How many kids in each group?
talkergroup_counts <- table(rawdata$talkergroup_final)
print(talkergroup_counts)/(275*4)
## 
##     0     1 
## 46200 40700
## 
##  0  1 
## 42 37
# CREATE A SINGLE SUMMARY WAVEFORM FOR N2P2 AND P3 ACROSS ALL PARTICIPANTS IN EACH GROUP
summary_waveforms <-
  rawdata %>%
  group_by(Stimulus, Time, talkergroup_final) %>%
  summarise(Average_N2P2 = mean(N2P2_waveform),Average_P3 = mean(P3_waveform) )
## `summarise()` has grouped output by 'Stimulus', 'Time'. You can override using
## the `.groups` argument.
# Define the X-axis vertical lines
vertical_lines1 <- c(180, 320, 550)  # Specify the X-axis positions
# Define the X-axis vertical lines
vertical_lines2 <- c(400, 750)  # Specify the X-axis positions

#Red
line_colors <- c("NeutGo" = "#D6E4F0", "NeutNoGo" = "#0000CD", "NegGo" = "#FFB6B6", "NegNoGo" = "#FF0000")
# line_colors <- c("NeutGo" = "lightskyblue1", "NeutNoGo" = "mediumblue", "NegGo" = "pink1", "NegNoGo" = "red2")

# DRAW THE GRAPHS
CWNS_N2P2_waveform <- ggplot(data = subset(summary_waveforms, talkergroup_final == 0), 
aes(x = Time, y = Average_N2P2, color = Stimulus)) +
  geom_line(stat = "identity", size = 1.5) +
  labs(x = "Time (ms)", y = "Amplitude in Microvolts (μV)") +
  scale_x_continuous(limits = c(-100, 900), breaks = seq(-100, 900, 100), position = "top") +
  scale_y_continuous(limits = c(-15, 2), breaks = seq(-14, 2, 2)) +
  scale_color_manual(values = line_colors, labels = c("Affective Go", "Affective NoGo","Neutral Go", "Neutral NoGo")) +  # Set line colors manually
  theme(
    axis.text = element_text(size = 14, color = "black"),
    axis.title = element_text(size = 14),
    plot.background = element_rect(fill = "white"),
    axis.line = element_line(linetype = "solid", color = "gray10", size = 1.4),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.text = element_text(size = 12), 
    legend.title =element_blank()
    ) +
  geom_vline(xintercept = vertical_lines1, color = "gray50", linetype = "dashed") +
  geom_hline(yintercept = 0, color = "gray70", linetype = "dotted") +
  geom_vline(xintercept = 0, color = "gray70", linetype = "dotted") +
  ggtitle("CWNS N2P2 Activity") +
  theme(plot.title = element_text(hjust = 0.5, size = 24, face = "bold", vjust = 2)) +
  theme(legend.position = c(.85, .2))  # Set the position of the legend
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot(CWNS_N2P2_waveform)
## Warning: Removed 96 rows containing missing values (`geom_line()`).

CWS_N2P2_waveform <- ggplot(data = subset(summary_waveforms, talkergroup_final == 1), 
aes(x = Time, y = Average_N2P2, color = Stimulus)) +
  geom_line(stat = "identity", size = 1.3) +
  labs(x = "Time (ms)", y = "Amplitude in Microvolts (μV)") +
  scale_x_continuous(limits = c(-100, 900), breaks = seq(-100, 900, 100), position = "top") +
  scale_y_continuous(limits = c(-15, 2), breaks = seq(-14, 2, 2)) +
  scale_color_manual(values = line_colors, labels = c("Affective Go", "Affective NoGo","Neutral Go", "Neutral NoGo")) +  # Set line colors manually
  theme(
    axis.text = element_text(size = 14 , color = "black"),
    axis.title = element_text(size = 14),
    plot.background = element_rect(fill = "white"),
    axis.line = element_line(linetype = "solid", color = "gray10", size = 1.4),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.text = element_text(size = 12), 
    legend.title =element_blank()
  ) +
  geom_vline(xintercept = vertical_lines1, color = "gray50", linetype = "dashed") +
  geom_hline(yintercept = 0, color = "gray70", linetype = "dotted") +
  geom_vline(xintercept = 0, color = "gray70", linetype = "dotted") +
  ggtitle("CWS N2P2 Activity") +
  theme(plot.title = element_text(hjust = 0.5, size = 24, face = "bold", vjust = 2)) +
  theme(legend.position = c(.85, .2))  # Set the position of the legend
plot(CWS_N2P2_waveform)  
## Warning: Removed 108 rows containing missing values (`geom_line()`).

CWNS_P3_waveform <- ggplot(data = subset(summary_waveforms, talkergroup_final == 0), 
aes(x = Time, y = Average_P3, color = Stimulus)) +
  geom_line(stat = "identity", size = 1.3) +
  labs(x = "Time (ms)", y = "Amplitude in Microvolts (μV)") +
  scale_x_continuous(limits = c(-100, 900), breaks = seq(-100, 900, 100), position = "top")+
  scale_y_continuous(limits = c(-2, 13), breaks = seq(-2, 13, 2)) +
  scale_color_manual(values = line_colors, labels = c("Affective Go", "Affective NoGo","Neutral Go", "Neutral NoGo")) +  # Set line colors manually
  theme(
    axis.text = element_text(size = 14, color = "black"),
    axis.title = element_text(size = 14),
    plot.background = element_rect(fill = "white"),
    axis.line = element_line(linetype = "solid", color = "gray10", size = 1.4),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.text = element_text(size = 12), 
    legend.title =element_blank()
  ) +
  geom_vline(xintercept = vertical_lines2, color = "gray50", linetype = "dashed") +
  geom_hline(yintercept = 0, color = "gray70", linetype = "dotted") +
  geom_vline(xintercept = 0, color = "gray70", linetype = "dotted") +
  ggtitle("CWNS P3 Activity") +
  theme(plot.title = element_text(hjust = 0.5, size = 24, face = "bold", vjust = 2)) +
    theme(legend.position = c(.65, .3))
plot(CWNS_P3_waveform)
## Warning: Removed 96 rows containing missing values (`geom_line()`).

CWS_P3_waveform <- ggplot(data = subset(summary_waveforms, talkergroup_final == 1),
aes(x = Time, y = Average_P3, color = Stimulus)) +
  geom_line(stat = "identity", size = 1.3) +
  labs(x = "Time (ms)", y = "Amplitude in Microvolts (μV)") +
  scale_x_continuous(limits = c(-100, 900), breaks = seq(-100, 900, 100), position = "top")+
  scale_y_continuous(limits = c(-2, 13), breaks = seq(-2, 13, 2)) +
  scale_color_manual(values = line_colors, labels = c("Affective Go", "Affective NoGo","Neutral Go", "Neutral NoGo")) +  # Set line colors manually
  theme(
    axis.text = element_text(size = 14 , color = "black"),
    axis.title = element_text(size = 14),
    plot.background = element_rect(fill = "white"),
    axis.line = element_line(linetype = "solid", color = "gray10", size = 1.4),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.text = element_text(size = 12), 
    legend.title =element_blank()
  ) +
  geom_vline(xintercept = vertical_lines2, color = "gray50", linetype = "dashed") +
  geom_hline(yintercept = 0, color = "gray70", linetype = "dotted") +
  geom_vline(xintercept = 0, color = "gray70", linetype = "dotted") +
  ggtitle("CWN P3 Activity") +
  theme(plot.title = element_text(hjust = 0.5, size = 24, face = "bold", vjust = 2)) +
  theme(legend.position = c(.65, .3))
plot(CWS_P3_waveform)  
## Warning: Removed 96 rows containing missing values (`geom_line()`).